5. Python String Manipulation
5. Python String Manipulation
STRING MANIPULATION
It can be enclosed within single quotation, double quotation, or triple quote
e.g. ‘ipcsguide’
“ipcsguide” ‘ipcsguide’
“””ipcsguide”””
‘how’ * 3 # howhowhow
3 * “map” # mapmapmap
‘2’ * 3 # 222
‘5’ * ‘4’ # Error
‘5’ + ‘4’ *3 # 5444
‘*’ * 4 # ****
s1 = ‘education’
‘h’ in ‘higher’ # True
‘p’ in ‘higher’ # False
‘a’ in s1 # True
‘b’ in s1 # False
‘cat’ in s1 # True
‘bat’ in s1 # False
‘D’ in s1 # False
‘tac’ in s1 # False
ord(‘A’) # 65
ord(‘e’) # 101
ord(‘5’) # 53
chr(65) # ‘A’
chr(101) # ‘e’
chr(53) # ‘5’
myword # ipcsguide
myword[0] #i
myword[4] #g
myword[-1] #e
myword[8] #p
myword[0] #i
x= len(myword)
x #9
myword[0:3] #ipc
myword[4:7] #gui
myword[-7:-4] #csg
myword[ :3] # i p c (first 3 letters)
myword[6: ] # i d e (index 6 to last)
myword[ :3] + myword[3: ] # ipcsguide
Reverse a string
"computer"[ : : -2] #retupmoc
FUNCTIONS
a. capitalize( )
Convert the first character of string into uppercase and all other into lower case
S1 = “ipcsgUIde is youtube CHANNEL”
S1.capitalize( ) # 'Ipcsguide is youtube channel'
"computer".capitalize( ) # Computer
"god is great".capitalize( ) # God is great
"India IS GReat cOUNTry".capitalize( ) # India is great country
b. isalnum( )
If the string is alphanumeric (alphabet or numbers), it will return True. String
should have atleast one character.
"KVS123".isalnum( ) # True
"KVS".isalnum( ) # True
"123".isalnum( ) # True
"123*".isalnum( ) # False
" ".isalnum( ) # False
"$".isalnum( ) # False
c. isalpha( )
If the string consists of alphabets, it will return True
"KVS123".isalpha( ) # False
"KVS".isalpha( ) # True
"123".isalpha( ) # False
"123*".isalpha( ) # False
" ".isalpha( ) # False
"$".isalpha( ) # False
d. isdigit( )
If the string consists of numbers, it will return True
"KVS123".isdigit( ) # False
"KVS".isdigit( ) # True
"123".isdigit( ) # True
"123*".isdigit( ) # False
" ".isdigit( ) # False
"$".isdigit( ) # False
f. isupper( )
If string consist has all characters in upper case, if character exists
"KVS123".isupper( ) # True
"KVS".isupper( ) # True
"kvs".isupper( ) # False
"kvs123".isupper( ) # False
"KVS123*".isupper( ) # True
"123".isupper( ) # False
"123*".isupper( ) # False
"ABcd".isupper( ) # False
"HE is Great".isupper( ) # False
g. isspace( )
If there is one or more space, returns true.
“”.isspace() # False
“ “.isspace( ) # True
“ “.isspace( ) # True
“my friend”.isspace( ) # False
h. lower( )
It return the string changed to lower case. Original string remains unchanged.
“IPCSGUIDE”.lower( ) # ipcsguide
S1 = “IndiAn”
S1.lower( ) # indian
S1 # IndiAn
i. upper( )
It return the string changed to upper case. Original string remains unchanged.
“IpCsGuide”.upper( ) # IPCSGUIDE
S1 = “IndiAn”
S1.uppper( ) # INDIAN
S1 # IndiAn
S1 = S1.upper( ) #Changed to original String
S1 # INDIAN
j. lstrip( )
Used to remove left side blank spaces of string.
e.g. “ Hello” “Hello”
>>> print("hello",fruit.lstrip(),"good") hello apple good
l. strip( )
used to remove all blank spaces (both left or right side) of string.
e.g. “ Hello ” “Hello”
>>> print("hello",fruit.strip(),"good") hello apple good
m. title( )
used to convert the first character in each word to Uppercase and remaining
characters to Lowercase.
e.g. “ >>> x = "this is my pen"
>>> print(x.title()) This Is My Pen
n. find( )
The find() method finds the first occurrence of the specified value. The find( )
method returns -1 if the value is not found
e.g. “ >>> x = "this is my pen"
>>> x.find("is") 2 (First occurrence of is)
>>> x.find("my") 8
>>> x.find("ok") -1
o. replace( )
Python String replace() Method The replace() method replaces a specified phrase
with another specified phrase
>>> str = "Winner is Sachin"
>>> x = str.replace("Sachin", "Dhoni")
>>> print(x) Winner is Dhoni
p. partition( )
Search for a given word and return tuple with three element
>>> txt = "My position in race is first"
>>> x = txt.partition("race")
>>> print(x) ('My position in ', 'race', ' is first')
q. split( )
Split a string into a list where each word is a list item:
>>> str = "today we are going for picnic"
>>> x = str.split()
>>> print(x) ['today', 'we', 'are', 'going', 'for', 'picnic']
Q1. Write a program in python to print the number of upper case characters and lowers case
characters present in a line of string
Q2. Write a program in python to input a string and check whether it is a palindrome or not.
Q3. Write a program in python to input a string and print the number of vowels present in
that string.
Q4. WAP in Python to input a string and count the number of times word “he” occurred