KEMBAR78
Python Strings.pptx
M Vishnuvardhan
String
String is basically represents sequence of characters. Technically string is a
sequence of Unicode characters in Python. Strings are written in single or double
quotes. Strings in Python are immutable i.e., modifying the string is not possible
Eg: name='Python’ or name= "Python".
Python allows to create multi line strings using ‘’’ or “””
Eg: msg = """ Python is a Object oriented
Language ""“
: msg = ‘’’ Python is a Object oriented
Language ‘’’
M Vishnuvardhan
Accessing chars in String
Individual characters of a string can be accessed using indexing. Index is a
integer number representing the position of character in the string it starts from
0 and ends at n-1 (n-length)
Eg: name= "Python".
print(name[0]) #returns P
print(name[2]) #returns t
print(name[7]) # index error occurs
Note: Python allows negative indexing on strings. Negative index starts
at -1 and go p to –n.
print(name[-1)) #prints n
M Vishnuvardhan
String – Slicing
Slicing operator (:) is used to get range of indexes. When specifying a range, the
return value will be a new string with the specified items.
Syntax stringName [ start : end ] # start is inclusive and end is exclusive
Eg: name= "Programming"
name[2:5] # returns ogr
If start is skipped then returns items from beginning, If end is skipped then
returns items till end .Slicing allows negative indexing also
name[-6:-3] # returns amm
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10
M Vishnuvardhan
String – Advanced Slicing
Syntax stringName [ start : end: step ]
# start is inclusive and end is exclusive and step is increment
Eg: name= "Programming«
[low:high:+ve] – normal order
name[2:10:2] # returns ormi
[high:low:-ve] – reverse order
name[10:2:-1] # returns gnimmarg
name[::1] # returns Programming
name[::-1] # returns gnimmargorP
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10
M Vishnuvardhan
String – Basic Operations
Python Expression Results Description
len(“Python”) 6 Length
“Python” + “Program” “PythonProgram” Concatenation
Py* 4 “PyPyPy” Repetition
y in “Python” True Membership
for x in “Python”:
print x
P y t h o n Iteration
M Vishnuvardhan
String methods
Method Description
count() Returns the number of times a specified value occurs in a string
find()
Searches the string for a specified value and returns the position of
where it was found
format() Formats specified values in a string
index()
Searches the string for a specified value and returns the position of
where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
islower() Returns True if all characters in the string are lower case
M Vishnuvardhan
String methods
Method Description
isnumeric() Returns True if all characters in the string are numeric
isupper() Returns True if all characters in the string are upper case
join() Converts the elements of an iterable into a string
lower() Converts a string into lower case
replace()
Returns a string where a specified value is replaced with a specified
value
split() Splits the string at the specified separator, and returns a list
strip() Returns a trimmed version of the string
title() Converts the first character of each word to upper case
upper() Converts a string into upper case
M Vishnuvardhan
String Formatting
format() method allows you to format selected parts of a string. It takes the
passed arguments, formats them, and places them in the string where the
placeholders {} are:
Syntax: string.format(value1, value2...)
The placeholders can be identified using named indexes {price}, numbered
indexes {0}, or even empty placeholders {}.
Eg:
txt1 = "Python {name}, is {ver}".format(name = "Language", ver = 3.10)
txt2 = "Python {0}, is {1}".format("Language",3.10)
txt3 = "Python {}, is {}".format("Language",3.10)
M Vishnuvardhan
String Escape Sequences
Code Result
' Single Quote
 Backslash
n New Line
r Carriage Return
t Tab
b Backspace
f Form Feed
ooo Octal value
xhh Hex value
M Vishnuvardhan

Python Strings.pptx

  • 2.
    M Vishnuvardhan String String isbasically represents sequence of characters. Technically string is a sequence of Unicode characters in Python. Strings are written in single or double quotes. Strings in Python are immutable i.e., modifying the string is not possible Eg: name='Python’ or name= "Python". Python allows to create multi line strings using ‘’’ or “”” Eg: msg = """ Python is a Object oriented Language ""“ : msg = ‘’’ Python is a Object oriented Language ‘’’
  • 3.
    M Vishnuvardhan Accessing charsin String Individual characters of a string can be accessed using indexing. Index is a integer number representing the position of character in the string it starts from 0 and ends at n-1 (n-length) Eg: name= "Python". print(name[0]) #returns P print(name[2]) #returns t print(name[7]) # index error occurs Note: Python allows negative indexing on strings. Negative index starts at -1 and go p to –n. print(name[-1)) #prints n
  • 4.
    M Vishnuvardhan String –Slicing Slicing operator (:) is used to get range of indexes. When specifying a range, the return value will be a new string with the specified items. Syntax stringName [ start : end ] # start is inclusive and end is exclusive Eg: name= "Programming" name[2:5] # returns ogr If start is skipped then returns items from beginning, If end is skipped then returns items till end .Slicing allows negative indexing also name[-6:-3] # returns amm -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 P r o g r a m m i n g 0 1 2 3 4 5 6 7 8 9 10
  • 5.
    M Vishnuvardhan String –Advanced Slicing Syntax stringName [ start : end: step ] # start is inclusive and end is exclusive and step is increment Eg: name= "Programming« [low:high:+ve] – normal order name[2:10:2] # returns ormi [high:low:-ve] – reverse order name[10:2:-1] # returns gnimmarg name[::1] # returns Programming name[::-1] # returns gnimmargorP -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 P r o g r a m m i n g 0 1 2 3 4 5 6 7 8 9 10
  • 6.
    M Vishnuvardhan String –Basic Operations Python Expression Results Description len(“Python”) 6 Length “Python” + “Program” “PythonProgram” Concatenation Py* 4 “PyPyPy” Repetition y in “Python” True Membership for x in “Python”: print x P y t h o n Iteration
  • 7.
    M Vishnuvardhan String methods MethodDescription count() Returns the number of times a specified value occurs in a string find() Searches the string for a specified value and returns the position of where it was found format() Formats specified values in a string index() Searches the string for a specified value and returns the position of where it was found isalnum() Returns True if all characters in the string are alphanumeric isalpha() Returns True if all characters in the string are in the alphabet isdecimal() Returns True if all characters in the string are decimals isdigit() Returns True if all characters in the string are digits islower() Returns True if all characters in the string are lower case
  • 8.
    M Vishnuvardhan String methods MethodDescription isnumeric() Returns True if all characters in the string are numeric isupper() Returns True if all characters in the string are upper case join() Converts the elements of an iterable into a string lower() Converts a string into lower case replace() Returns a string where a specified value is replaced with a specified value split() Splits the string at the specified separator, and returns a list strip() Returns a trimmed version of the string title() Converts the first character of each word to upper case upper() Converts a string into upper case
  • 9.
    M Vishnuvardhan String Formatting format()method allows you to format selected parts of a string. It takes the passed arguments, formats them, and places them in the string where the placeholders {} are: Syntax: string.format(value1, value2...) The placeholders can be identified using named indexes {price}, numbered indexes {0}, or even empty placeholders {}. Eg: txt1 = "Python {name}, is {ver}".format(name = "Language", ver = 3.10) txt2 = "Python {0}, is {1}".format("Language",3.10) txt3 = "Python {}, is {}".format("Language",3.10)
  • 10.
    M Vishnuvardhan String EscapeSequences Code Result ' Single Quote Backslash n New Line r Carriage Return t Tab b Backspace f Form Feed ooo Octal value xhh Hex value
  • 11.