Python String
Python String
STRINGS
• Strings are created by enclosing various characters within quotes. Strings
are one of the most popular data types
• Python does not distinguish between single quotes and double quotes.
Ex:
V1=“hello python”
V2=‘welcome’
V3=“””this is triple quoted string”””
Print v1
Print v2
Print v3
Output
Hello python
Welcome
This is triple quoted string
Prepared by S.Radha Priya 2
• Strings are of literal or scalar type
• Python interpreter treats string as a single value
• Strings are immutable. If we want to change an element of a string, we
have to create a new string.
• Triple quoted strings can span to multiple lines
>>>v1=“””welcome
to
python
programming”’’
>>>print v1
output
welcome
to
python
programming
• Strings are made up of smaller pieces of characters. The data types that
are made up of smaller pieces are known as compound data types.
• To access a part of the string ([]) must be used.
>>>string=“hello”
>>>letter=string[4]
>>>print letter #5th letter of string
Output
O
• len is a built-in function in python. When used with a string, len returns
the length or number of characters in the string.
Ex:
>>>v1=“hello python!”
>>>len(v1)
Output
13
Access the last letter of out string
>>>v1=“hello python!”
>>>l=len(v1) #hello python!
last=v1[-1]
print last
output
!
Prepared by S.Radha Priya 5
Ex:
>>>var=“hello world”
last=var[-1] #negative indices for accessing the string from last
second_last=var[-2] #second last element of the string
print last
print second_last
Output
d
l
3. String Slices
• A piece or subset of a string is known as slice
• Slice operator is applied to a string with the use of square braces([])
• Operator [n:m] will give a substring consists of letters between n and m
indices[i.e., nth index to (m-1)th index.
• Operator [n:m:s] nth index to (m-1)th index, where s is called the step
value ie., n,n+s, n+2s,n+3s……..
Prepared by S.Radha Priya 6
Ex:
>>>var=‘hello python’
>>>print var[0:4]
>>>print var[6:12]
Output
hell
python
>>>al=‘abcdefghij’
>>>print al[1:8:3]
>>>print al[1:8:2]
Output
beh
bdfh
>>>var=‘banana’
>>>var[:4]
>>>var[4:0]
>>>var[ : ]
Prepared by S.Radha Priya 7
>>>var=‘banana’
>>>var[4:3]
>>>var[ : :-1]
Output
bana
na
‘banana’
‘’ #the second index is smaller than first index, then the
output will be empty and represented in single quotes.
‘ananab’ #step is -1, and no value for n and m, then it will print the
string in reverse order.
An empty string has a length 0. though it does not contain any character, it is
still a string.
5. String Traversal
Traversal is a process in which we access all the elements of the string one by
one using some conditional statements such as for loop, while loop etc.,
A function find takes a string and a character as input. A while loop traverses
the string until the end and compares every element of the string with the
character passed by the user. If it matches any element of the string then the
index of that element is returned by the function. otherwise it returns -1.
Note: the return statement in a while loop works in the same way as the
break statement.
Prepared by S.Radha Priya 13
6. ESCAPE CHARACTERS
\newline ignored
\\ Backslash(\)
\’ Single quote(‘)
\” Double quote(“)
\a ASCII bell
\f ASCII backspace
\n ASCII linefeed
txt = "Hello\nWorld!"
print(txt)
output
Hello
World!
txt = "Hello\rWorld!"
print(txt)
Output
Hello
World!
Prepared by S.Radha Priya 17
txt = "Hello\tWorld!"
print(txt)
Output
Hello World!
Output
The first letter of python is p
The sum=-15
The sum=-15
The sum=15
11 is the octal equivalent of 9
c is the hexadecimal equivalent of 12
C is the hexadecimal equivalent of 12
8.983540e+00 is the exponential equivalent of 8.983540
8.983540E+00 is the exponential equivalent of 8.983540
Prepared by S.Radha Priya 20
8.String formatting functions
Built-in functions for strings
1. capitalize()
makes the first letter of the string capital
ex:
a='banana‘
x=a.capitalize()
print(x)
Output:Banana
2. center(width,fillchar())
Returns a space-padded string with the original string centred to a total
width columns.
Ex:
a=‘banana’
x=a.center(20)
print(x)
Output:
--7 blanksplace-- banana ---7 blank space----
Prepared by S.Radha Priya 21
Ex:
a='banana‘
x=a.center(20,"0")
print(x)
output
0000000banana0000000
Ex:
a='hello, and welcome to my world‘
x=a.capitalize()
print(x)
output
Hello, and welcome to my world
3.count(str,beg=0,end=len(string())
counts the number of times string occurs in the string or in a
substring provided that starting index is beg and ending index is end.
Syntax: string.count(value,start,end)
Ex:
List1=[2,-1,0,-2,8]
List2=*‘lilly’, ‘jasmine’, ‘rose’+
List3=*‘python’, 5.5,8+
List4=*‘python’, 5.6, *20,40++ list is contained in another list.ie nested list
>>>org1=[1,2,3,4]
cp1=org1[:]
Print cp1
Output
[1,2,3,4]
Making changes in the original list
>>>org1.append(10)
>>>print org1
[1,2,3,4,10] #original list is changed
>>>print cp1
[1,2,3,4] #copied list is unchanged
• The value of any element inside the list can be changed at any point of
time
• The elements accessible by their index value.
• Index value starts with 0 and ends with n-1
Ex:
list=[10,20,30,40]
Print list[1]
Output
20
Changing the value in the list
List[3]=50
Print list
Output
[10,20,30,50]
a. Pop operator
Ex:
List=[10,20,30,40]
a=list.pop(2)
Print list
Print a
output
[10,20,40]
30
The pop operator deletes the element on the provided index and stores that
element in a variable for further use.
b. Del Operator
The del operator deletes the value on the provided index, but it does not
store the value for further use.
Prepared by S.Radha Priya 31
Ex:
List=*‘W’,’X’,’Y’,’Z’+
del list(1)
Print list
Output
*‘w’,’y’,’z’+
c.Remove operator
This operator is used to remove or delete from the list.
Ex:
List=[10,20,30,40]
List.remove(10)
Print.list
Output
[20,30,40]
Prepared by S.Radha Priya 32
Delete more than one value from a list, del operator with slicing is used.
Ex:
List=[1,2,3,4,5,6,7,8]
del list[1:3]
Print list
Output
[1,4,5,6,7,8]
5. pop() method removes the specified index,(or the last item if index is not
specified)
Ex:
List=*“apple”, ”banana”, “cherry”+
List.pop()
Print(list)
Output
*‘apple’, ’banana’+
Ex:
P=[1,4,2,9,7,8,9,3,1]
X=p.count(9)
Print(x)
Output
2
11. reverse() method- method reverse the sorting order of the elements.
Ex:
Fruits=*‘apple’,’banana’,’cherry’+
Fruits.reverse()
Print(fruits)
Output:
*‘cherry’, ’banana’, ’apple’+
13. max(list)- it returns the item that has the maximum value in the list.
14. min(list)- it returns the item that has the minimum value in the list.
Ex:
Cars=*‘ford’, ‘bmw’, ‘volvo’+
Max(cars)
Min(cars)
Output
Volvo
bmw
Prepared by S.Radha Priya 42