How To Use Python String Functions With Examples

How To Use Python String Functions With Examples
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 

👉C++ String Function 

👉C String Function 

👉Java String Function 


How To Create String in Python?

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


Access Index Python String: 
Python String can be used print string using print() function.
We can also print particular characters of a string using an index number.

How To Store String in array Form in  Python?

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 


Access character slicing from the string. A Python string can access a range of characters from a string using slicing operator colon(:).

How To Slicing String in  Python?

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 


Update String: A Python String Update can be updated string value by reassigning a new value to the same variable. python string can not update the particular character of the string.

How To Update String in  Python?

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


Python can not be Update String in  Python?

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


Multiline String: Python Multiline string can create a multi-line string using three double quotes.

How To Use Multiline String in  Python?

s="""icoderweb Learning Website"""

print("Multiline String Print:",s)

*****OUTPUT*****
Multiline String Print: icoderweb Learning Website 


Search String: Python Search string in keyword is used to check specified character or group of characters is present or not.

How To Use Search String in  Python?

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


How To Use Search String in  Python?

s="icoderweb Learning Website"

print("Learning" in s)

print("Python" in s)

*****OUTPUT*****
True

False


String Concatenation: Python String Concatenate can combine two or more than two strings using the plus (+) operator.

How To Use Concatenate String in  Python?

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 


Python can not be Concatenate number String in Python?

Book="Python"

Price=450

s3="Programming Book"+Book+" is +Price

print(s3)

*****OUTPUT*****
TypeError: can only concatenate str (not "int") to str


Format Function: Python combines string with numeric value format() function is used.

How to Use Format Concatenate number String in Python?

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 


String repetition: Python asterisk (*) symbol can be used to contact multiple copies of the same string.

How to Use Asterisk Symbols String in Python?

s="icoderweb"

print(s*5)

*****OUTPUT*****
 icoderweb icoderweb  icoderweb  icoderweb  icoderweb 



Python String All Methods And Functions 

Python contains the following string methods.

1. capitalize: Python String capitalize function can convert the first letter of the string into uppercase.

How to Use capitalize function String in Python?

s="icoderweb Website"

print(s.capitalize())

*****OUTPUT*****
Icoderweb Website


2. case fold: 
Python String case fold function can convert a string into lowercase.

How to Use case fold function String in Python?

s="Icoderweb Website"

print(s.casefold())

*****OUTPUT*****
icoderweb website


3. center
: Python String center function can be used to align the string to the center.
It has two parameters width and fill char in which fill char is optional.

How to Use center function String in Python?

s="Icoderweb Website"

print("Original String:",s)

print("Center String:",s.center())

*****OUTPUT*****
Original String:Icoderweb Website

Center String:            Icoderweb Website


4. ends with: Python String ends with function can be
returns the boolean value True and False.
If the given string ends with specified string returns true otherwise false.

How to Use endswith function String in Python?

s="Icoderweb Website"

print(s.endswith(Icoderweb))

print(s.endswith("Website"))

*****OUTPUT*****
False

True 


5. starts with: Python String function can return the boolean value True and false.
It is given string starts with the specified string that returns true otherwise false.

How to Use startswith function String in Python?

s="Icoderweb"

print(s.startswith("Icoderweb"))

print(s.startswith("icoderweb"))

*****OUTPUT*****
True

False 


6. find
: Python String find function can be used to search the specified string in a string.
If the specified string is found then it returns the position of where it is found.


How to Use find function String in Python?

s="Icoderweb Learning Website"

print(s.find("Icoderweb"))

print(s.find("Icoderweb",5))

print("Website",10))

print(s.find("Learning",3,8))

*****OUTPUT*****

-1 

19 

-1 


7.index:Python String index function is same as find but it raises an error when the specified string is not found.

How to Use index function String in Python?

s="Icoderweb Learning Website"

print(s.index("Icoderweb"))

print(s.index("Icoderweb",5))

print("Website",10))

print(s.index("Learning",3,8))

*****OUTPUT*****

-1 

19 

-1 


8.format:
Python String function can be used to format the string.it can be insert specified value inside the string using format () function.

A specified value is inserted inside the string using a placeholder.

A placeholder is identified using numbered indexes {0} or empty placeholders{}.


How to Use format function String in Python?

s="Icoderweb"

g="Good Morning"

print("My Website Name is{} {}".format(s,g))

*****OUTPUT*****
My Website Name is Icoderweb Good Morning


9. album: Python String isalnum function can be used to check specified string is alphanumeric or not.

The string that contains only the alphabet and number is called alphanumeric. It returns the boolean values True and False.

How to Use isalnum function String in Python?

s1="Icoderweb"

s2="Icoder.web"

print(s.isalnum())

print(s.isalnum())

*****OUTPUT*****
True

False 


10. isalpha: 
Python String isalpha function can be used to check specified string is alphabetic or not.

It returns the boolean value True and False.
it can return true if the string is alphabetic otherwise returns false.

How to Use isalpha function String in Python?

s1="Icoderweb"

s2="Icoder.web"

print(s.isalpha())

print(s.isalpha())

*****OUTPUT*****
True

False 


11. decimal: 
Python String is a decimal function that can be
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.


How to Use isdecimal function String in Python?

s1="Icoderweb"

s2="807714"

print(s.isdecimal())

print(s.isdecimal())

*****OUTPUT*****
False 

True


12. digit: 
Python String isdigit function can be used to check all the characters of the string are digit or not.
It returns the boolean values True and False. It can return true if all characters are digit otherwise returns false.

How to Use isdigit function String in Python?

s1="Icoderweb"

s2="807714"

print(s.isdigit())

print(s.isdigit())

*****OUTPUT*****
False 

True


13. identifier: 
Python String identifier function can be returns True if the specified string is valid identifier otherwise returns False.

How to Use isidentifier function String in Python?

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


14. is lower: Python String is a lower function that can be returns True if all the characters of the string are in lowercase otherwise returns False.

How to Use islower function String in Python?

s1="Icoderweb"

s2="icoderweb"

print(s1.islower())

print(s2.islower())

*****OUTPUT*****
True 

False


15. is numeric: 
Python String is a numeric function that can be returned True if all the characters of the string are numeric character otherwise returns False.

How to Use isnumeric function String in Python?

s1="Icoderweb"

s2="807715"

print(s1.isnumeric())

print(s2.isnumeric())

*****OUTPUT*****
False

True 


16.isupper: 
Python String isupper function can be
returns True if all the characters are in uppercase otherwise returns False.


How to Use isupper function String in Python?

s1="icoderweb"

s2="ICODERWEB"

print(s1.isupper())

print(s2.islower())

*****OUTPUT*****
False

True 


17. space: 
Python String is a space function that can be
returns True if all the characters are white space otherwise returns False.

How to Use isspace function String in Python?

s1="icoderweb"

s2=" "

print(s1.isspace())

print(s2.isspace())

*****OUTPUT*****
False

True 


18. len: 
Python String len function can be used to the length of the string.

How to Use len function String in Python?

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


19. lower: 
Python String lower function can be used to convert all the characters of a string to Lower case.

How to Use lower function String in Python?

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


20. upper: 
Python String upper function can be used to convert all the characters of a string to Upper case.

How to Use upper function String in Python?

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


21. swap case: 
Python String swap case function can be
converts lowercase characters into uppercase and uppercase characters into lowercase.

How to Use swapcase function String in Python?

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 


22. strip: Python String strip function can remove unwanted white space from the string.

How to Use strip function String in Python?

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 


23. strip: Python String strip function can remove the left side unwanted white space from the string.

How to Use lstrip function String in Python?

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 


24. strip
: Python String strip function can be
removes right side unwanted white space from the string.

How to Use rstrip function String in Python?

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 


25. replace: 
Python String replace function can be
replaces the old string with a new string.

How to Use replace function String in Python?

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


26.split: Python String split function can be used to break the sentence into words using a separator.
A default separator is a white space. the split function can be a returns list.

How to Use split function String in Python?

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']




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.