String Datatype in Python
String Datatype in Python
Ques : What is a String? What are the different ways to create a String in Python?
A string is a sequence of characters. A String in python can be created using single quotes (''),
double quotes ("") and triple quotes (' ' ') / ( " "" ) . When we use triple quotes, strings can span
several lines without using the escape character.
Example:
st r1 = ' H el l o, " H ow ar e yo u " ? '
st r1 = "H el l o, \ " H ow a r e y ou \ " ? "
st r1 = '' ' H el l o, " H o w a r e y ou " ? '' '
st r1 = "" " H el l o, " H o w ar e y ou " ? " ""
st r2 = "C om pu t e r \ n Appl i cati on s "
st r3 = "" " Of fi ci al w e bsi te o f Py th on l an g u ag e i s:
h ttp:// ww w. py th on . o rg/ "" "
Ques : How are the indexes allotted in a String in Python? How can we access
individual characters in a String in Python?
Python indexes the characters in a string from left to right and from right end to left. From left
to right, the first character of a string has the index 0 and from right to left, the first character
of a string is –1. Individual characters in a string can be accessed by specifying the string name
followed by the index number in square brackets ([]).
String/Characters C O M P U T E R
st r1 = 'C O MP UT E R'
pri n t ( st r 1[0]) # 'C '
pri n t ( st r 1[7]) # 'R '
pri n t ( st r 1[3]) # 'P '
pri n t ( st r 1[ - 6] ) # 'M '
pri n t ( st r 1[ - 4] ) # 'U '
pri n t ( st r 1[ - 1] ) # 'R '
Page 1 of 7
Strings in Python
Ques : How can you replicate a String? Give example.
String can be replicated any number of times with the asterisk "*" operator.
st rA = 'P yth on '
pri n t ( st r A*4) # 'P yth on P yth on P yt h on Pyt h o n ‘
pri n t ( ' =' * 3 0) # '= == = == == == = == == == == == == = == == == '
Operator Description
in The in operator tests if one string is present in another string. If a string exists then it
returns True, otherwise False. For example,
not in The not in operator evaluates True if it does not find a sub string in the specified
sequence, otherwise False. For example,
Operator Description
is Is operator evaluate to True if the variables on either side of the operator point to the
same object and False otherwise.
s1 = s2 = "h rm s"
s1 i s s2 #T ru e
is not Is not operator evaluate to False if the variables on either side of the operator point
to the same object and True otherwise.
Page 2 of 7
Strings in Python
Ques : How can we compare two Strings in Python?
Strings can be compared with the relational (comparison) operators ==, !=, <, >, <= , >=
These comparisons use the standard character-by-character comparison rules for ASCII or
Unicode. The comparison operator returns a Boolean result True or False.
Operator Name Str1 = "Py", Result
Str2 = "Python"
== Equal to Str 1= =St r2 False
!= Not equal to Str 1!= St r2 True
> Greater than Str 1> St r2 False
< Less than Str 1< St r2 True
>= Greater than or equal to Str 1> = St r2 False
<= Less than or equal to Str 1< =St r2 True
0 1 2 3 4 5 6 7 8
H a p p i n e s s
-9 -8 -7 -6 -5 -4 -3 -2 -1
Page 3 of 7
Strings in Python
Operator Description
Ques: Consider the following string and Write statements for the following:
Function Description
len(str) Returns length of string str. Length includes white spaces.
str.find (sub, start, end ) Returns the index of the substring sub if found in a string str. If not
found, it returns -1.
sub - It's the substring to be
searched in the string str. st r = ' pyt h on p r og r a m'
start – where to begin the pri n t ( st r .fi n d ('p ') ) # 0
search. starting index is pri n t ( st r .fi n d ('p ' , 5)) # 7
pri n t ( st r .fi n d (' s ')) # -1
assumed to be 0 if not given
pri n t ( st r .fi n d (' P ')) # -1
end – ending index assumed pri n t ( st r .fi n d ('p r o g')) #7
as length of str if not given
str.count (sub, start, end ) Returns the number of occurrences of a substring sub in the given
string str.
sub - It's the substring to be
searched in the string str. st r = ' st ri n g i s i mm u tabl e '
pri n t ( st r .c ou n t ( 'i ' )) #3
Page 4 of 7
Strings in Python
start – starting index, pri n t ( st r .c ou n t ( 'i ' ,5)) #2
assumed to be 0 if not given va r = ' Th i s i s an e x ampl e '
end – ending index, pri n t ( va r .c ou n t ( 'i s' )) #2
assumed as length of str if pri n t ( va r .c ou n t ( 'i s' ,5 )) #1
not given
str.isalnum() Returns True if all characters in the string str are alphanumeric
(either alphabets or numbers). If not, it returns False.
n am e = ' Ch a rl e s1 23 '
pri n t(n a m e.i s al n u m( )) # T ru e
n am e= ' Ch a rl e s 1 2 3'
pri n t(n a m e.i s al n u m( )) # Fal s e ( co n tai n s spa c e)
pri n t(" 123 " .i sal n u m ()) # Tru e
n am e= ' Ch a rl e s @12 3 '
pri n t(n a m e.i s al n u m( )) # Fal s e ( co n tai n s @ )
str.isalpha() Returns True if all characters in the string str are alphabets. If not,
it returns False.
str.isdigit() Returns True if all characters in a string str are digits. If not, it
returns False.
Page 5 of 7
Strings in Python
str.isspace() Returns True if there are only whitespace characters in the string
str. If not, it return False.
st r= ' '
pri n t ( st r .i ss pa c e() ) # T ru e
st r= ' \t '
pri n t ( st r .i ss pa c e() ) # T ru e
st r= ' \n '
pri n t ( st r .i ss pa c e() ) # T ru e
st r = ' wel c o m e '
pri n t ( st r .i ss pa c e() ) #F al s e
st r = ' '
pri n t ( st r .i ss pa c e() ) #F al s e
str.lstrip() It returns the string after removing the spaces on the left of str.
st r = ' H an sr aj S ch o ol '
pri n t ( st r) # H an s r aj S ch o ol
pri n t ( st r .l st ri p() ) # H an s raj S ch o ol
str.rstrip() It returns the string after removing the spaces on the right of str.
str.replace(old, new, max) Replaces all the occurrences of substring ‘old’ with ‘new’ in the
string str. ‘max’ is the maximum no, of replacements
st r = ' w o rk i s w o r sh i p'
pri n t ( st r . r epl ac e (' w ', ' W ')) # W o rk i s W o r sh i p
pri n t ( st r . r epl ac e (' ' ,' # ')) # wo r k #i s# wo r sh i p
a = "T h i s i s i sl an d o f i st an bu l "
pri n t ( a. r e pl ac e( "i s" ," wa s" , 3 )) # Thwas was wasland of istanbul
Page 6 of 7
Strings in Python
Ques: Observe the following assignment statement and answer the questions that
follow:
str = '''be
the change'''
b e \n t h e c h a n g e
Page 7 of 7