Unit II Python Strings
Unit II Python Strings
Unit II –
Python String
• Notice that the input is not evaluated. We want to store the typed
characters, not to evaluate them as a Python expression.
0 1 2 3 4 5 6 7
8 greet = "Hello Bob"
>>>
>>> greet[0]
'H'
>>> print(greet[0], greet[2], greet[4])
Hlo
>>> x = 8
>>> print(greet[x - 2])
B
0 1 2 3 4 5 6 7
•8 In a string of n characters, the last character is at position n-
1 since we start counting with 0.
• We can index from the right side using negative indexes.
>>> greet[-1]
'b'
>>> greet[-3]
'B'
Slicing:
<string>[<start>:<end>]
0 1 2 3 4 5 6 7
8
>>> greet[0:3]
'Hel'
>>> greet[5:9]
' Bob'
>>> greet[:5]
'Hello'
>>> greet[5:]
' Bob'
>>> greet[:]
'Hello Bob'
-5 -4 -3 -2 -1
• Out of range indices are ignored for slicing
• when start and end have the same sign, if start >=end,
empty slice is returned
Why?
b) o
d) tw
a) rockrockrock
b) rocksrocksrocks
d) error
\\ Backslash Output
hello world
\n New Line Octal Value Escape Character
To escape a byte of octal value character, use a preceding backslash
\r Carriage Return for three digit octal value in the string.
x = '\101\102\103'
\t Tab print(x)
Output
\b Backspace ABC
\f Form Feed Octal value 101 represents A, 102 represents B, and so on. So, in
the output, we got ABC for the given octal values.
\ooo Octal Value
Hex Value Escape Character
\xhh Hex Value To specify a byte using hex value, use a preceding backslash and x for two
digit hex value in the string.
x = '\x41\x42\x43'
print(x)
Output
ABC
Hex value of 41 means 65 in decimal. And a byte with decimal 65
represents the character A. Similarly 42 is B, 43 is C.
M.Sc.(CA) Dept. ACBCS College, Nashik -Rahul Sonawane
String Special College
Fergusson Operators
• Assume string variable a holds 'Hello' and variable b holds 'Python',
then-
>>> "%d" % 1
'1'
>>> "%-5d" % 1
'1 ‘
>>> "%x" % 17
'11'
>> "%#x" % 17
'0x11'
>>> "%#X" % 17
'0X11'
>>> "%d" % 1
'1'
>>> "%03d" % 1
'001'
Result
str.center(40, 'a') : aaaathis is string example....wow!!!aaaa
#!/usr/bin/python3
Result str1 = "this is string example....wow!!!"
15 str2 = "exam";
15 print (str1.find(str2))
-1 print (str1.find(str2, 10))
print (str1.find(str2, 40))
• Example
• The following example shows the usage of len() method.
• #!/usr/bin/python3
Result
• str = "this is string example....wow!!!"
Length of the string: 32
• print ("Length of the string: ", len(str))a
Example
#!/usr/bin/python3 Result
str = "this is string example....wow!!!" ['this', 'is', 'string', 'example....wow!!!']
print (str.split( )) ['th', 's is string example....wow!!!']
print (str.split('i',1)) ['this is string example....', 'o', '!!!']
print (str.split('w'))
Solution:
str1 = ‘Computer'
print("Original String is", str1)
res = str1[0]
l = len(str1)
mi = int(l / 2)
res = res + str1[mi]
res = res + str1[l - 1]
print("New String:", res)
• Example:
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
print (list[2:]) # Prints elements starting from 3rd element
#Output:
#[2.23, 'john', 70.2]
l1=[1,2,[4,4,5]]
print l1
#Output: [1, 2, [4, 4, 5]]
Output
List inside the nested list: ['a', 1] First
element of the sublist: a
Output:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 : ['physics',
'chemistry', 2000]
Output:
[40, 200, 300]
Example
#!/usr/bin/python3
list1 = ['physics', 'chemistry', 'maths']
print (len(list1))
list2=list(range(5)) #creates list of numbers between 0-4
print (len(list2))
When we run above program, it produces
following result-
3
5
Example
#!/usr/bin/python3
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]
print ("Max value element : ", max(list1))
print ("Max value element : ", max(list2))
When we run above program, it produces
following result-
Max value element : Python
Max value element : 700
Example
#!/usr/bin/python3
list1, list2 = ['C++','Java', 'Python'], [456, 700, 200]
print ("min value element : ", min(list1))
print ("min value element : ", min(list2))
When we run above program, it produces
following result
min value element : C++
min value element : 200
• Given a list L that contains numbers between 1 and 100, create a new list whose
first element is how many ones are in L, whose second element is how many twos are
in L, etc.
frequencies = []
for i in range(1,101):
frequences.append(L.count(i))
Visit Us
http://www.aef.edu.in/acbcs
M.Sc.(CA) Dept. ACBCS College, NashikCenter For Business And Computer Studies,
Ashoka -Rahul Sonawane
Nashik