- Python contains the following string methods.
- It converts the first letter of the string into uppercasse.
str="easy softwares"
print(str.capitalize())
"""
***Output***
Easy softwares
"""
- It converts string into lowercase.
str="Easy Softwares"
print(str.casefold())
"""
***Output***
easy softwares
"""
- It is used to align the string to the center.
- It has two parameters width and fillchar in which fillchar is optional.
#Example1
str="Easy"
print("Original String:",str)
#without fillchar
print("Centered String:",str.center(20))
"""
***Output***
Original String: Easy
Centered String: Easy
"""
#example2
str="Easy"
print("Original String:",str)
#with fillchar
#filling space with @
print("Centered String:",str.center(10,"@"))
"""
***Output***
Original String: Easy
Centered String: @@@Easy@@@
Note:total width of the output is 10
"""
- It returns boolean value(True/False).
- It the given string ends with specified string returns true otherwise false.
str="Easy Softwares"
print(str.endswith("softwares"))
print(str.endswith("Softwares"))
"""
***Output***
False
True
"""
- It returns boolean value(True/False).
- It the given string starts with specified string returns true otherwise false.
str="Easy Softwares"
print(str.startswith("easy"))
print(str.startswith("Easy"))
"""
***Output***
False
True
"""
- It is 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.
str="you can learn python easily."
#statrting index of python is 14
#this line will search python in string
print(str.find("python"))
#this line will search python from index 10
print(str.find("python",10))
#this line will search python from index 15
print(str.find("python",15))
#this line will search can between index 3 to 10
print(str.find("can",3,10))
"""
***Output***
14
14
-1
4
"""
- This method is same as find but it raises an error when the specified string is not found.
str="you can learn python easily."
#statrting index of python is 14
#this line will search python in string
print(str.index("python"))
#this line will search python from index 10
print(str.index("python",10))
#this line will search can between index 3 to 10
print(str.index("can",3,10))
"""
***Output***
14
14
-1
4
"""
- It is used to format the string.
- We can insert specified value inside the string using format () method.
- The specified value is inserted inside string using placeholder.
- The placeholder is identified using numbered indexes {0} or empty placeholders{}.
#Example 1:Empty placeholder
name="Tom"
pro="Python"
print("my name is {} and i love {}".format(name,pro))
"""
***Output***
my name is Tom and i love Python
"""
#Example 2:Numbered indexes
name="Tom"
pro="Python"
print("my name is {0} and i love {1}".format(name,pro))
"""
***Output***
my name is Tom and i love Python
"""
#Example 3:Reverse indexes
#focus on output for better understanding
name="Tom"
pro="Python"
print("my name is {1} and i love {0}".format(name,pro))
"""
***Output***
my name is Python and i love Tom
"""
- It is used to check specified string is alphanumeric or not.
- String that contains only alphabet and number is called alphanumeric.
- It returns boolean value(True/False).
str1="easy123"
str2="easy@123"
print(str1.isalnum())
print(str2.isalnum())
"""
***Output***
True
False
"""
- It is used to check specified string is alphabetic or not.
- It returns boolean value(True/False).
- It returns true if string is alphabetic otherwise returns false.
str1="easy"
str2="easy@123"
print(str1.isalpha())
print(str2.isalpha())
"""
***Output***
True
False
"""
- It is used to check all the characters of string are decimal or not.
- It returns boolean value(True/False).
- It returns true if all characters are decimal otherwise returns false.
str1="easy"
str2="123"
print(str1.isdecimal())
print(str2.isdecimal())
"""
***Output***
False
True
"""
- It is used to check all the characters of string are digit or not.
- It returns boolean value(True/False).
- It returns true if all characters are digit otherwise returns false.
str1="easy"
str2="123"
print(str1.isdigit())
print(str2.isdigit())
"""
***Output***
False
True
"""
- It returns true if the specified string is valid identifier otherwise returns false.
str1="easy" #valid
str2="123" #invalid
str3="abc123"#valid
str4="*abc" #invalid
str5="ab@cd" #invalid
print(str1.isidentifier())
print(str2.isidentifier())
print(str3.isidentifier())
print(str4.isidentifier())
print(str5.isidentifier())
"""
***Output***
True
False
True
False
False
"""
- It returns true if all the characters of the string is in lowercase otherwise returns false.
str1="python"
str2="Python"
print(str1.islower())
print(str2.islower())
"""
***Output***
True
False
"""
- It returns true if all the characters of the string are numeric character otherwise returns false.
str1="python"
str2="12345"
print(str1.isnumeric())
print(str2.isnumeric())
"""
***Output***
False
True
"""
- It returns true if all the characters are in uppercase otherwise returns false.
str1="python"
str2="PYTHON"
print(str1.isupper())
print(str2.isupper())
"""
***Output***
False
True
"""
- It returns true if all the characters are white space otherwise returns false.
str1=" "
str2="PYTHON"
print(str1.isspace())
print(str2.isspace())
"""
***Output***
True
False
"""
- It is used to length of the string.
str1="Easy"
str2="Pyhton"
print("Length of str1:",len(str1))
print("Length of str2:",len(str2))
"""
***Output***
Length of str1: 4
Length of str2: 6
"""
- It is used to convert all the characters of a string to Lower case.
str1="Easy"
str2="PyTHON"
print("str1:",str1.lower())
print("str2:",str2.lower())
"""
***Output***
str1: easy
str2: python
"""
- It is used to convert all the characters of a string to Upper case.
str1="Easy"
str2="PyTHON"
print("str1:",str1.upper())
print("str2:",str2.upper())
"""
***Output***
str1: EASY
str2: PYTHON
"""
- It converts lowercase characters into uppercase and uppercase characters into lowercase.
str1="Easy"
str2="PyTHON"
print("str1:",str1.swapcase())
print("str2:",str2.swapcase())
"""
***Output***
str1: eASY
str2: pYthon
"""
- It removes unwanted white-space from string.
str=" PyTHON "
print("Without strip:",str,"Programming")
print("With strip:",str.strip(),"Programming")
"""
***Output***
Without strip: PyTHON Programming
With strip: PyTHON Programming
"""
- It removes left side unwanted white-space from string.
str=" PyTHON "
print("Without strip:",str,"Programming")
print("With strip:",str.lstrip(),"Programming")
"""
***Output***
Without strip: PyTHON Programming
With strip: PyTHON Programming
"""
- It removes right side unwanted white-space from string.
str=" PyTHON "
print("Without strip:",str,"Programming")
print("With strip:",str.rstrip(),"Programming")
"""
***Output***
Without strip: PyTHON Programming
With strip: PyTHON Programming
"""
- It replaces the old string with new string.
str="I love Java"
print("Original String:",str)
#replacing java with python
str=str.replace("Java","Python")
print("New String:",str)
"""
***Output***
Original String: I love Java
New String: I love Python
"""
- It is used to break the sentenc into words using separator.
- The default separator is white space.
- split() function returns list.
str="I love Python"
print("Original String:",str)
mylist=str.split();
print("New String:",mylist)
"""
***Output***
Original String: I love Python
New String: ['I', 'love', 'Python']
Note:New string is in the form of list
"""
- We can use any characater as a separator.
#asterisk as a separator
str="I*love*Python"
print("Original String:",str)
mylist=str.split("*");
print("New String:",mylist)
"""
***Output***
Original String: I love Python
New String: ['I', 'love', 'Python']
Note:New string is in the form of list
"""
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.
0 Comments