SlideShare a Scribd company logo
PYTHON PROGRAMMING
Department of computer science
• Introduction to Python and installation:
• Python is a widely used general-purpose, high level
programming language. It was initially designed by
Guido van Rossum in 1991 and developed by Python
Software Foundation. It was mainly developed for
emphasis on code readability, and its syntax allows
programmers to express concepts in fewer lines of
code. Python is a programming language that lets
you work quickly and integrate systems more
efficiently. There are two major Python versions-
Python 2 and Python 3.
• • On 16 October 2000, Python 2.0 was released with
many new features.
• • On 3rd December 2008, Python 3.0 was released
with more testing and includes new features.
• Beginning with Python programming:
• 1) Finding an Interpreter:
• Before we start Python programming, we need to
have an interpreter to interpret and run our
programs. There are certain online interpreters like
https://ide.geeksforgeeks.org/,
http://ideone.com/ or http://codepad.org/ that
can be used to start Python without installing an
interpreter.
• Windows: There are many interpreters available
freely to run Python scripts like IDLE (Integrated
Development Environment) which is installed
when you install the python software from
http://python.org/downloads/
• 2) Writing first program:
• # Script Begins
• Statement1
• Statement2
• Statement3
• # Script Ends
• Why to use Python:
• The following are the primary factors to
use python in day-to-day life:
• 1. Python is object-oriented
• Structure supports such concepts as
polymorphism, operation overloading and
multiple inheritance.
• 2. Indentation
• Indentation is one of the greatest feature
in python
• 3. It’s free (open source)
• Downloading python and installing python is free and
easy
• 4. It’s Powerful
• Dynamic typing
• Built-in types and tools
• Library utilities
• Third party utilities (e.g. Numeric, NumPy, sciPy)
• Automatic memory management
• 5. It’s Portable
• Python runs virtually every major platform used today
• As long as you have a compatable python interpreter
installed, python programs will run in exactly the same
manner, irrespective of platform.
• 6. It’s easy to use and learn
• No intermediate compile
• Python Programs are compiled automatically to an intermediate form
called byte code, which the interpreter then reads.
• This gives python the development speed of an interpreter without
the performance loss inherent in purely interpreted languages.
• Structure and syntax are pretty intuitive and easy to grasp.
• 7. Interpreted Language
• Python is processed at runtime by python Interpreter
• 8. Interactive Programming Language
• Users can interact with the python interpreter directly for writing the
programs
• 9. Straight forward syntax
• The formation of python syntax is simple and straight forward which
also makes it popular.
• Installation:
• There are many interpreters available freely to
run Python scripts like IDLE (Integrated
Development Environment) which is installed
when you install the python software from
http://python.org/downloads/
• Steps to be followed and remembered:
• Step 1: Select Version of Python to Install. Step 2:
Download Python Executable Installer. Step 3:
Run Executable Installer.
• Step 4: Verify Python Was Installed On Windows.
• Step 5: Verify Pip Was Installed. Step 6:
Add Python Path to Environment Variables
(Optional)
• Working with Python Python Code Execution:
• Python’s traditional runtime execution
model:Source code you type is translated to byte
code, which is then run by the Python Virtual
Machine (PVM). Your code is automatically
compiled, but then it is interpreted.
• There are two modes for using the Python
interpreter:
• • Interactive Mode
• • Script Mode
• Running Python in interactive mode: Without passing
python script file to the interpreter, directly execute
code to Python prompt.
• Once you‟re inside the python interpreter, then you
can start.
• >>> print("hello world")
• hello world
• #Relevant output is displayed on subsequent lines
without the >>> symbol
• >>> x=[0,1,2]
• #Quantities stored in memory are not displayed by
default.
• >>> x
• #If a quantity is stored in memory, typing its name will
display it. [0, 1, 2]
• >>> 2+3 5
The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the
python interpreter uses to indicate that it is ready. If the programmer types 2+6,
the interpreter replies 8.
• Running Python in script mode:
• Alternatively, programmers can store Python script
source code in a file with the .py extension, and use the
interpreter to execute the contents of the file. To
execute the script by the interpreter, you have to tell
the interpreter the name of the file. For example, if you
have a script name MyFile.py and you're working on
Unix, to run the script you have to type:
• python MyFile.py
• Working with the interactive mode is better when
Python programmers deal with small pieces of code as
you can type and execute them immediately, but when
the code is more than 2-4 lines, using the script for
coding can help to modify and use the code in future.
• Example:
PYTHON PROGRAMMING.pptx
• Numeric Data types:
• The data stored in memory can be of many types.
For example, a student roll number is stored as a
numeric value and his or her address is stored as
alphanumeric characters. Python has various
standard data types that are used to define the
operations possible on them and the storage
method for each of them.
• Int:
• Int, or integer, is a whole number, positive or
negative, without decimals, of unlimited length.
• >>> print(24656354687654+2)
• 24656354687656
• >>> print(20)
• 20
• # To verify the type of any object in
Python, use the type() function:
• >>> type(10)
• <class 'int'>
• >>> a=11
• >>> print(type(a))
• <class 'int'>
• Float:
• Float, or "floating point number" is a number,
positive or negative, containing one or more
decimals.
• Float can also be scientific numbers with an "e" to
indicate the power of 10.
• >>> y=2.8
• >>> y
• 2.8
• >>> y=2.8
• >>> print(type(y))
• <class 'float'>
• >>> type(.4)
• <class 'float'>
• >>> 2.
• 2.0
• Example:
• x = 35e3
• y = 12E4
• z = -87.7e100
• print(type(x))
• print(type(y))
• print(type(z))
• Output:
• <class 'float'>
• <class 'float‘>
• <class 'float'>
• Boolean:
• Objects of Boolean type may have one of
two values, True or False:
• >>> type(True)
• <class 'bool'>
• >>> type(False)
• <class 'bool'>
• String:
• 1. Strings in Python are identified as a contiguous set
of characters represented in the quotation marks.
Python allows for either pairs of single or double
quotes.
• • 'hello' is the same as "hello".
• • Strings can be output to screen using the print
function.
• For example: print("hello").
• >>> print(“VSR college") VSR college
• >>> type(“VSR college")
• <class 'str'>
• >>> print(‘VSR college')
• VSR college
• >>> " "
• ' '
• A string is a group/a sequence of characters. Since
Python has no provision for arrays, we simply use
strings. This is how we declare a string. We can use
a pair of single or double quotes. Every string
object is of the type „str‟.
• >>> type("name")
• <class 'str'>
• fruit = 'banana'
• >>> letter = fruit[1]
• The second statement selects character number 1
from fruit and assigns it to letter. The expression in
brackets is called an index. The index indicates
which character in the sequence we want
• String slices:
• A segment of a string is called a slice.
Selecting a slice is similar to selecting a
character: Subsets of strings can be taken
using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the
string and working their way from -1 at the
end. Slice out substrings, sub lists, sub Tuples
using index. Syntax:[Start: stop: steps]
• Slicing will start from index and will go up to
stop in step of steps.
• Default value of start is 0,
• Stop is last index of list
• And for step default is 1
• For example 1−
• str = 'Hello World!'
• print str
• # Prints complete string
• print str[0]
• # Prints first character of the string
• print str[2:5]
• # Prints characters starting from 3rd to 5th
• print str[2:]
• # Prints string starting from 3rd character
• print str * 2
• # Prints string two times
• print str + "TEST"
• # Prints concatenated string
• Output:
• Hello World!
• H
• llo
• llo World!
• Hello World!Hello World!
• Hello World!TEST
• Example 2:
• >>> x='computer'
• >>> x[1:4]
• 'omp'
• >>> x[3:]
• 'puter'
• >>> x[:5]
• 'compu'
• >>> x[-1]
• 'r'
• >>> x[-3:]
• 'ter'
• >>> x[:-2]
• 'comput'
• >>> x[::-2]
• 'rtpo'
• >>> x[::-1]
• 'retupmoc'
• Immutability:
• It is tempting to use the [] operator on the left side of an
assignment, with the intention of changing a character in
a string.
• For example:
• >>> greeting=‘VSR college!'
• >>> greeting[0]='n'
• TypeError: 'str' object does not support item assignment
The reason for the error is that strings are immutable,
which means we can‟t change an existing string.
• The best we can do is creating a new string that is a
variation on the original:
• >>> greeting = 'Hello, world!'
• >>>new_greeting = 'J' + greeting[1:]
• >>>new_greeting
• 'Jello, world!'
• Note: The plus (+) sign is the string concatenation
operator and the asterisk (*) is the repetition operator
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
• Note: All the string methods will be returning
either true or false as the result
• 1. isalnum():
• Isalnum() method returns true if string has at
least 1 character and all characters are
alphanumeric and false otherwise.
• Syntax:
• String.isalnum()
• Example:
• >>> string="123alpha"
• >>>string.isalnum()
• True
• 2. isalpha():
• isalpha() method returns true if string has
at least 1 character and all characters are
alphabetic and false otherwise.
• Syntax:
• String.isalpha()
• Example:
• >>> string="nikhil"
• >>>string.isalpha()
• True
• 3. isdigit():
• isdigit() returns true if string contains only
digits and false otherwise.
• Syntax:
• String.isdigit()
• Example:
• >>> string="123456789"
• >>>string.isdigit()
• True
• 3. isdigit():
• isdigit() returns true if string contains only
digits and false otherwise.
• Syntax:
• String.isdigit()
• Example:
• >>> string="123456789"
• >>>string.isdigit()
• True
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx

More Related Content

Similar to PYTHON PROGRAMMING.pptx (20)

PPTX
Introduction to Programming.pptx ok ok ok
846Sarthakpandey
 
PDF
Python workshop
Jibin Sabu
 
PPTX
introduction to python programming concepts
GautamDharamrajChouh
 
PPTX
Python knowledge ,......................
sabith777a
 
PDF
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
shetoooelshitany74
 
PPT
notwa dfdfvs gf fdgfgh s thgfgh frg reggg
Godwin585235
 
PPTX
Introduction to learn and Python Interpreter
Alamelu
 
PDF
Python Programming
Saravanan T.M
 
PDF
Tutorial on-python-programming
Chetan Giridhar
 
PDF
Python Programing Bio computing,basic concepts lab,,
smohana4
 
PDF
05 python.pdf
SugumarSarDurai
 
PPTX
Introduction to python programming ( part-1)
Ziyauddin Shaik
 
PDF
Python quick guide
Hasan Bisri
 
PPTX
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
PPT
Python - Module 1.ppt
jaba kumar
 
PPTX
Python
Gagandeep Nanda
 
PPTX
1. python programming
sreeLekha51
 
PPTX
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
PPTX
Python Tutorial for Beginner
rajkamaltibacademy
 
PPTX
Python Demo.pptx
ParveenShaik21
 
Introduction to Programming.pptx ok ok ok
846Sarthakpandey
 
Python workshop
Jibin Sabu
 
introduction to python programming concepts
GautamDharamrajChouh
 
Python knowledge ,......................
sabith777a
 
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
shetoooelshitany74
 
notwa dfdfvs gf fdgfgh s thgfgh frg reggg
Godwin585235
 
Introduction to learn and Python Interpreter
Alamelu
 
Python Programming
Saravanan T.M
 
Tutorial on-python-programming
Chetan Giridhar
 
Python Programing Bio computing,basic concepts lab,,
smohana4
 
05 python.pdf
SugumarSarDurai
 
Introduction to python programming ( part-1)
Ziyauddin Shaik
 
Python quick guide
Hasan Bisri
 
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
Python - Module 1.ppt
jaba kumar
 
1. python programming
sreeLekha51
 
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
Python Tutorial for Beginner
rajkamaltibacademy
 
Python Demo.pptx
ParveenShaik21
 

More from swarna627082 (6)

PPT
jghj.ppt
swarna627082
 
PPTX
dgp.pptx
swarna627082
 
PPTX
dsfkh.pptx
swarna627082
 
PPTX
dsf.pptx
swarna627082
 
PPTX
ababc.pptx
swarna627082
 
DOCX
Data and Information.docx
swarna627082
 
jghj.ppt
swarna627082
 
dgp.pptx
swarna627082
 
dsfkh.pptx
swarna627082
 
dsf.pptx
swarna627082
 
ababc.pptx
swarna627082
 
Data and Information.docx
swarna627082
 
Ad

Recently uploaded (20)

PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Controller Request and Response in Odoo18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Introduction presentation of the patentbutler tool
MIPLM
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Ad

PYTHON PROGRAMMING.pptx

  • 2. • Introduction to Python and installation: • Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently. There are two major Python versions- Python 2 and Python 3. • • On 16 October 2000, Python 2.0 was released with many new features. • • On 3rd December 2008, Python 3.0 was released with more testing and includes new features.
  • 3. • Beginning with Python programming: • 1) Finding an Interpreter: • Before we start Python programming, we need to have an interpreter to interpret and run our programs. There are certain online interpreters like https://ide.geeksforgeeks.org/, http://ideone.com/ or http://codepad.org/ that can be used to start Python without installing an interpreter. • Windows: There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) which is installed when you install the python software from http://python.org/downloads/
  • 4. • 2) Writing first program: • # Script Begins • Statement1 • Statement2 • Statement3 • # Script Ends
  • 5. • Why to use Python: • The following are the primary factors to use python in day-to-day life: • 1. Python is object-oriented • Structure supports such concepts as polymorphism, operation overloading and multiple inheritance. • 2. Indentation • Indentation is one of the greatest feature in python
  • 6. • 3. It’s free (open source) • Downloading python and installing python is free and easy • 4. It’s Powerful • Dynamic typing • Built-in types and tools • Library utilities • Third party utilities (e.g. Numeric, NumPy, sciPy) • Automatic memory management • 5. It’s Portable • Python runs virtually every major platform used today • As long as you have a compatable python interpreter installed, python programs will run in exactly the same manner, irrespective of platform.
  • 7. • 6. It’s easy to use and learn • No intermediate compile • Python Programs are compiled automatically to an intermediate form called byte code, which the interpreter then reads. • This gives python the development speed of an interpreter without the performance loss inherent in purely interpreted languages. • Structure and syntax are pretty intuitive and easy to grasp. • 7. Interpreted Language • Python is processed at runtime by python Interpreter • 8. Interactive Programming Language • Users can interact with the python interpreter directly for writing the programs • 9. Straight forward syntax • The formation of python syntax is simple and straight forward which also makes it popular.
  • 8. • Installation: • There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) which is installed when you install the python software from http://python.org/downloads/ • Steps to be followed and remembered: • Step 1: Select Version of Python to Install. Step 2: Download Python Executable Installer. Step 3: Run Executable Installer. • Step 4: Verify Python Was Installed On Windows.
  • 9. • Step 5: Verify Pip Was Installed. Step 6: Add Python Path to Environment Variables (Optional)
  • 10. • Working with Python Python Code Execution: • Python’s traditional runtime execution model:Source code you type is translated to byte code, which is then run by the Python Virtual Machine (PVM). Your code is automatically compiled, but then it is interpreted.
  • 11. • There are two modes for using the Python interpreter: • • Interactive Mode • • Script Mode
  • 12. • Running Python in interactive mode: Without passing python script file to the interpreter, directly execute code to Python prompt. • Once you‟re inside the python interpreter, then you can start. • >>> print("hello world") • hello world • #Relevant output is displayed on subsequent lines without the >>> symbol • >>> x=[0,1,2] • #Quantities stored in memory are not displayed by default. • >>> x • #If a quantity is stored in memory, typing its name will display it. [0, 1, 2] • >>> 2+3 5
  • 13. The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter replies 8.
  • 14. • Running Python in script mode: • Alternatively, programmers can store Python script source code in a file with the .py extension, and use the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the interpreter the name of the file. For example, if you have a script name MyFile.py and you're working on Unix, to run the script you have to type: • python MyFile.py • Working with the interactive mode is better when Python programmers deal with small pieces of code as you can type and execute them immediately, but when the code is more than 2-4 lines, using the script for coding can help to modify and use the code in future. • Example:
  • 16. • Numeric Data types: • The data stored in memory can be of many types. For example, a student roll number is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. • Int: • Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. • >>> print(24656354687654+2) • 24656354687656 • >>> print(20) • 20
  • 17. • # To verify the type of any object in Python, use the type() function: • >>> type(10) • <class 'int'> • >>> a=11 • >>> print(type(a)) • <class 'int'>
  • 18. • Float: • Float, or "floating point number" is a number, positive or negative, containing one or more decimals. • Float can also be scientific numbers with an "e" to indicate the power of 10. • >>> y=2.8 • >>> y • 2.8 • >>> y=2.8 • >>> print(type(y)) • <class 'float'> • >>> type(.4) • <class 'float'>
  • 19. • >>> 2. • 2.0 • Example: • x = 35e3 • y = 12E4 • z = -87.7e100 • print(type(x)) • print(type(y)) • print(type(z)) • Output: • <class 'float'> • <class 'float‘> • <class 'float'>
  • 20. • Boolean: • Objects of Boolean type may have one of two values, True or False: • >>> type(True) • <class 'bool'> • >>> type(False) • <class 'bool'>
  • 21. • String: • 1. Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. • • 'hello' is the same as "hello". • • Strings can be output to screen using the print function. • For example: print("hello"). • >>> print(“VSR college") VSR college • >>> type(“VSR college") • <class 'str'> • >>> print(‘VSR college') • VSR college • >>> " " • ' '
  • 22. • A string is a group/a sequence of characters. Since Python has no provision for arrays, we simply use strings. This is how we declare a string. We can use a pair of single or double quotes. Every string object is of the type „str‟. • >>> type("name") • <class 'str'> • fruit = 'banana' • >>> letter = fruit[1] • The second statement selects character number 1 from fruit and assigns it to letter. The expression in brackets is called an index. The index indicates which character in the sequence we want
  • 23. • String slices: • A segment of a string is called a slice. Selecting a slice is similar to selecting a character: Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. Slice out substrings, sub lists, sub Tuples using index. Syntax:[Start: stop: steps] • Slicing will start from index and will go up to stop in step of steps. • Default value of start is 0,
  • 24. • Stop is last index of list • And for step default is 1 • For example 1− • str = 'Hello World!' • print str • # Prints complete string • print str[0] • # Prints first character of the string • print str[2:5] • # Prints characters starting from 3rd to 5th • print str[2:] • # Prints string starting from 3rd character • print str * 2 • # Prints string two times • print str + "TEST" • # Prints concatenated string
  • 25. • Output: • Hello World! • H • llo • llo World! • Hello World!Hello World! • Hello World!TEST
  • 26. • Example 2: • >>> x='computer' • >>> x[1:4] • 'omp' • >>> x[3:] • 'puter' • >>> x[:5] • 'compu' • >>> x[-1] • 'r' • >>> x[-3:] • 'ter' • >>> x[:-2] • 'comput' • >>> x[::-2] • 'rtpo' • >>> x[::-1] • 'retupmoc'
  • 27. • Immutability: • It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. • For example: • >>> greeting=‘VSR college!' • >>> greeting[0]='n' • TypeError: 'str' object does not support item assignment The reason for the error is that strings are immutable, which means we can‟t change an existing string. • The best we can do is creating a new string that is a variation on the original: • >>> greeting = 'Hello, world!' • >>>new_greeting = 'J' + greeting[1:] • >>>new_greeting • 'Jello, world!' • Note: The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator
  • 30. • Note: All the string methods will be returning either true or false as the result • 1. isalnum(): • Isalnum() method returns true if string has at least 1 character and all characters are alphanumeric and false otherwise. • Syntax: • String.isalnum() • Example: • >>> string="123alpha" • >>>string.isalnum() • True
  • 31. • 2. isalpha(): • isalpha() method returns true if string has at least 1 character and all characters are alphabetic and false otherwise. • Syntax: • String.isalpha() • Example: • >>> string="nikhil" • >>>string.isalpha() • True
  • 32. • 3. isdigit(): • isdigit() returns true if string contains only digits and false otherwise. • Syntax: • String.isdigit() • Example: • >>> string="123456789" • >>>string.isdigit() • True
  • 33. • 3. isdigit(): • isdigit() returns true if string contains only digits and false otherwise. • Syntax: • String.isdigit() • Example: • >>> string="123456789" • >>>string.isdigit() • True