How To Use Python String Functions With Examples |
How To Use Python String Functions With Examples
Python String: A String is a sequence of characters.A python String can be created single quotes(' ') and triple quotes(''' ''').
String Lenght can be started (0). python String can be created as follows.
single quotes s1='icoderweb'
Double quotes s2="icoderweb"
triple quotes s3='''icoderweb'''
Important Some Others Programming Language String
s1='icoderweb'#Single quotes
s2="icoderweb"#Double quotes
s3=' ' 'icoderweb ' ' '#Triple quotes
print("Single quotes:",s1)
print("Double quotes:",s1)
print("Triple quotes:",s1)
*****OUTPUT*****
Single quotes:icoderweb
Double quotes:icoderweb
Triple quotes:icoderweb
s1='icoderweb'#Single quotes
s2="icoderweb"#Double quotes
s3=' ' 'icoderweb ' ' '#Triple quotes
print("Single quotes:",s1)
print("Double quotes:",s1)
print("Triple quotes:",s1)
*****OUTPUT*****
Single quotes:icoderweb
Double quotes:icoderweb
Triple quotes:icoderweb
We can also print particular characters of a string using an index number.
s='icoderweb'
print("String Print:",s)
print("First Character Print:",s[0])
print("Second Character Print:",s[2])
print("Third Character Print:",s[-1])
*****OUTPUT*****
String Print: icoderweb
First Character Print: i
Second Character Print: o
Third Character Print: b
s='icoderweb'
print("String Print:",s)
print("First Character Print:",s[0])
print("Second Character Print:",s[2])
print("Third Character Print:",s[-1])
*****OUTPUT*****
String Print: icoderweb
First Character Print: i
Second Character Print: o
Third Character Print: b
s='icoderweb Learning Website'
print("String Print:",s)
print("First Line Print:",s[2:5])
print("Second Line Print:",s[0:8])
print("Third Line Print:",s[9:-1])
*****OUTPUT*****
String Print: icoderweb Learning Website
First Line Print: ode
Second Line Print: icoderwe
Third Line Print: Learning Websit
s='icoderweb Learning Website'
print("String Print:",s)
print("First Line Print:",s[2:5])
print("Second Line Print:",s[0:8])
print("Third Line Print:",s[9:-1])
*****OUTPUT*****
String Print: icoderweb Learning Website
First Line Print: ode
Second Line Print: icoderwe
Third Line Print: Learning Websit
s='icoderweb Learning Website'
print("Original String Print:",s)
s="Python Language"
print("Update String Print:",s)
*****OUTPUT*****
Original String Print: icoderweb Learning Website
Update String Print: Python Language
s='icoderweb Learning Website'
print("Original String Print:",s)
s="Python Language"
print("Update String Print:",s)
*****OUTPUT*****
Original String Print: icoderweb Learning Website
Update String Print: Python Language
s='icoderweb Learning Website'
print("Original String Print:",s)
s[0]="P"
print("Update String Print:",s)
*****OUTPUT*****
Original String Print: icoderweb Learning Website
line 5, in
TypeError: 'str' object does not support item assignment
s='icoderweb Learning Website'
print("Original String Print:",s)
s[0]="P"
print("Update String Print:",s)
*****OUTPUT*****
Original String Print: icoderweb Learning Website
s="""icoderweb Learning Website"""
print("Multiline String Print:",s)
*****OUTPUT*****
Multiline String Print: icoderweb Learning Website
s="""icoderweb Learning Website"""
print("Multiline String Print:",s)
*****OUTPUT*****
Multiline String Print: icoderweb Learning Website
s="icoderweb Learning Website"
search_s=input("Enter any String To Search:")
if search_s in s:
print("Search String in Present:",search_s)
else:
print("Search String Not Present:",search_s)
*****OUTPUT*****
Enter any String To Search:
Search String in Present: Learn
Second Run Program
Enter any String To Search: Data
Search String Not Present: Data
s="icoderweb Learning Website"
search_s=input("Enter any String To Search:")
if search_s in s:
print("Search String in Present:",search_s)
else:
print("Search String Not Present:",search_s)
*****OUTPUT*****
Enter any String To Search:
Search String in Present: Learn
Second Run Program
Enter any String To Search: Data
Search String Not Present: Data
s="icoderweb Learning Website"
print("Learning" in s)
print("Python" in s)
*****OUTPUT*****
True
False
s="icoderweb Learning Website"
print("Learning" in s)
print("Python" in s)
*****OUTPUT*****
True
False
s1="icoderweb Learning "
s2="Programming Language"
s3=s1+s2
print("String First:",s1)
print("String Second:",s1)
print("String Third:",s1+s2)
*****OUTPUT*****
String First: icoderweb Learning
String Second: icoderweb Learning
String Third: icoderweb Learning Programming Language
s1="icoderweb Learning "
s2="Programming Language"
s3=s1+s2
print("String First:",s1)
print("String Second:",s1)
print("String Third:",s1+s2)
*****OUTPUT*****
String First: icoderweb Learning
String Second: icoderweb Learning
String Third: icoderweb Learning Programming Language
Book="Python"
Price=450
s3="Programming Book"+Book+" is +Price
print(s3)
*****OUTPUT*****
TypeError: can only concatenate str (not "int") to str
Book="Python"
Price=450
s3="Programming Book"+Book+" is +Price
print(s3)
*****OUTPUT*****
Book="Python"
Price=450
s="Book Name {} or Price is Rs. {}".format(Book,Price)print(s3)
*****OUTPUT*****
Book Name Python Language or Price is Rs. 450
Book="Python"
Price=450
print(s3)
*****OUTPUT*****
Book Name Python Language or Price is Rs. 450
s="icoderweb"
print(s*5)
*****OUTPUT*****
icoderweb icoderweb icoderweb icoderweb icoderweb
s="icoderweb"
print(s*5)
*****OUTPUT*****
icoderweb icoderweb icoderweb icoderweb icoderweb
1. capitalize: Python String capitalize function can convert the first letter of the string into uppercase.
s="icoderweb Website"
print(s.capitalize())
*****OUTPUT*****
Icoderweb Website
s="icoderweb Website"
print(s.capitalize())
*****OUTPUT*****
Icoderweb Website
s="Icoderweb Website"
print(s.casefold())
*****OUTPUT*****
icoderweb website
s="Icoderweb Website"
print(s.casefold())
*****OUTPUT*****
icoderweb website
It has two parameters width and fill char in which fill char is optional.
s="Icoderweb Website"
print("Original String:",s)
print("Center String:",s.center())
*****OUTPUT*****
Original String:Icoderweb Website
Center String: Icoderweb Website
s="Icoderweb Website"
print("Original String:",s)
print("Center String:",s.center())
*****OUTPUT*****
Original String:Icoderweb Website
Center String: Icoderweb Website
returns the boolean value True and False.
If the given string ends with specified string returns true otherwise false.
s="Icoderweb Website"
print(s.endswith(Icoderweb))
print(s.endswith("Website"))
*****OUTPUT*****
False
True
s="Icoderweb Website"
print(s.endswith(Icoderweb))
print(s.endswith("Website"))
*****OUTPUT*****
False
True
It is given string starts with the specified string that returns true otherwise false.
s="Icoderweb"
print(s.startswith("Icoderweb"))
print(s.startswith("icoderweb"))
*****OUTPUT*****
True
False
s="Icoderweb"
print(s.startswith("Icoderweb"))
print(s.startswith("icoderweb"))
*****OUTPUT*****
True
False
If the specified string is found then it returns the position of where it is found.
s="Icoderweb Learning Website"
print(s.find("Icoderweb"))
print(s.find("Icoderweb",5))
print("Website",10))
print(s.find("Learning",3,8))
*****OUTPUT*****
0
-1
19
-1
s="Icoderweb Learning Website"
print(s.find("Icoderweb"))
print(s.find("Icoderweb",5))
print("Website",10))
print(s.find("Learning",3,8))
*****OUTPUT*****
0
-1
19
-1
s="Icoderweb Learning Website"
print(s.index("Icoderweb"))
print(s.index("Icoderweb",5))
print("Website",10))
print(s.index("Learning",3,8))
*****OUTPUT*****
0
-1
19
-1
s="Icoderweb Learning Website"
print(s.index("Icoderweb"))
print(s.index("Icoderweb",5))
print("Website",10))
print(s.index("Learning",3,8))
*****OUTPUT*****
0
-1
19
-1
A specified value is inserted inside the string using a placeholder.
A placeholder is identified using numbered indexes {0} or empty placeholders{}.
s="Icoderweb"
g="Good Morning"
print("My Website Name is{} {}".format(s,g))
*****OUTPUT*****
My Website Name is Icoderweb Good Morning
s="Icoderweb"
g="Good Morning"
print("My Website Name is{} {}".format(s,g))
*****OUTPUT*****
My Website Name is Icoderweb Good Morning
The string that contains only the alphabet and number is called alphanumeric. It returns the boolean values True and False.
s1="Icoderweb"
s2="Icoder.web"
print(s.isalnum())
print(s.isalnum())
*****OUTPUT*****
True
False
s1="Icoderweb"
s2="Icoder.web"
print(s.isalnum())
print(s.isalnum())
*****OUTPUT*****
True
False
It returns the boolean value True and False.
s1="Icoderweb"
s2="Icoder.web"
print(s.isalpha())
print(s.isalpha())
*****OUTPUT*****
True
False
s1="Icoderweb"
s2="Icoder.web"
print(s.isalpha())
print(s.isalpha())
*****OUTPUT*****
True
False
used to check all the characters of a string are decimal or not.
It returns the boolean value True and False.it can return true if all characters are decimal otherwise returns false.
s1="Icoderweb"
s2="807714"
print(s.isdecimal())
print(s.isdecimal())
*****OUTPUT*****
False
True
s1="Icoderweb"
s2="807714"
print(s.isdecimal())
print(s.isdecimal())
*****OUTPUT*****
False
True
It returns the boolean values True and False. It can return true if all characters are digit otherwise returns false.
s1="Icoderweb"
s2="807714"
print(s.isdigit())
print(s.isdigit())
*****OUTPUT*****
False
True
s1="Icoderweb"
s2="807714"
print(s.isdigit())
print(s.isdigit())
*****OUTPUT*****
False
True
s1="Icoderweb"
s2="807714"
s3=xyz143"
s4="*xyz"
s5=xy@z
print(s1.identifier())
print(s2.identifier())
print(s3.identifier())
print(s4.identifier())
print(s5.identifier())
*****OUTPUT*****
True
False
True
False
False
s1="Icoderweb"
s2="807714"
s3=xyz143"
s4="*xyz"
s5=xy@z
print(s1.identifier())
print(s2.identifier())
print(s3.identifier())
print(s4.identifier())
print(s5.identifier())
*****OUTPUT*****
True
False
True
False
False
s1="Icoderweb"
s2="icoderweb"
print(s1.islower())
print(s2.islower())
*****OUTPUT*****
True
False
s1="Icoderweb"
s2="icoderweb"
print(s1.islower())
print(s2.islower())
*****OUTPUT*****
True
False
s1="Icoderweb"
s2="807715"
print(s1.isnumeric())
print(s2.isnumeric())
*****OUTPUT*****
False
True
s1="Icoderweb"
s2="807715"
print(s1.isnumeric())
print(s2.isnumeric())
*****OUTPUT*****
False
True
returns True if all the characters are in uppercase otherwise returns False.
s1="icoderweb"
s2="ICODERWEB"
print(s1.isupper())
print(s2.islower())
*****OUTPUT*****
False
True
s1="icoderweb"
s2="ICODERWEB"
print(s1.isupper())
print(s2.islower())
*****OUTPUT*****
False
True
returns True if all the characters are white space otherwise returns False.
s1="icoderweb"
s2=" "
print(s1.isspace())
print(s2.isspace())
*****OUTPUT*****
False
True
s1="icoderweb"
s2=" "
print(s1.isspace())
print(s2.isspace())
*****OUTPUT*****
False
True
s1="icoderweb"
s2="Python"
print("Lenght of String s1:",len(s1))
print("Lenght of String s2:",len(s2))
*****OUTPUT*****
Lenght of String s1:9
Lenght of String s2:6
s1="icoderweb"
s2="Python"
print("Lenght of String s1:",len(s1))
print("Lenght of String s2:",len(s2))
*****OUTPUT*****
Lenght of String s1:9
Lenght of String s2:6
s1="ICODERWEB"
s2="WEBSITE"
print("Lower of String s1:",s1.lower())
print("Lower of String s2:",s2.lower())
*****OUTPUT*****
Lower of String s1:icoderweb
Lower of String s2:website
s1="ICODERWEB"
s2="WEBSITE"
print("Lower of String s1:",s1.lower())
print("Lower of String s2:",s2.lower())
*****OUTPUT*****
Lower of String s1:icoderweb
Lower of String s2:website
s1="icoderweb"
s2="website"
print("Upper of String s1:",s1.upper())
print("Upper of String s2:",s2.upper())
*****OUTPUT*****
Upper of String:ICODERWEB
Upper of String:WEBSITE
s1="icoderweb"
s2="website"
print("Upper of String s1:",s1.upper())
print("Upper of String s2:",s2.upper())
*****OUTPUT*****
Upper of String:ICODERWEB
Upper of String:WEBSITE
converts lowercase characters into uppercase and uppercase characters into lowercase.
s1="Icoderweb"
s2="WEBSITE"
print("Swap of String s1:",s1.swapcase())
print("Swap of String s2:",s2.swapcase())
*****OUTPUT*****
Swap of String s1: iCODERWEB
Swap of String s2: website
s1="Icoderweb"
s2="WEBSITE"
print("Swap of String s1:",s1.swapcase())
print("Swap of String s2:",s2.swapcase())
*****OUTPUT*****
Swap of String s1: iCODERWEB
Swap of String s2: website
s=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.strip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
s=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.strip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
s=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.lstrip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
s=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.lstrip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
removes right side unwanted white space from the string.
s1=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.rstrip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
s1=" Icoderweb "
print("Original String s:",s,Website)
print("Strip String s:",s.rstrip())
*****OUTPUT*****
Original String s: Icoderweb Website
Strip String s: Icoderweb Website
replaces the old string with a new string.
s="Icoderweb Best Website"
s=s.replace("Best","Learning")
print("Original String s:",s)
print("Strip String s:",s)
*****OUTPUT*****
Original String s:Icoderweb Best Website
New String s: Icoderweb Learning Website
s="Icoderweb Best Website"
s=s.replace("Best","Learning")
print("Original String s:",s)
print("Strip String s:",s)
*****OUTPUT*****
Original String s:Icoderweb Best Website
New String s: Icoderweb Learning Website
A default separator is a white space. the split function can be a returns list.
s="Icoderweb Best Website"
print("Original String s:",s)
slist=a.split()
print("New String s:",slist)
*****OUTPUT*****
Original String s:Icoderweb Best Website
New String s:[ 'Icoderweb', 'Best', 'Website']
s="Icoderweb Best Website"
print("Original String s:",s)
slist=a.split()
print("New String s:",slist)
*****OUTPUT*****
Original String s:Icoderweb Best Website
New String s:[ 'Icoderweb', 'Best', 'Website']