In this python tutorial, you will learn about the different built-in string methods in Python. Here, we will see various python string methods that can be used in Python string.
Also we will check:
- String methods in python
- String title method python
- String casefold method python
- String center method python
- String isdigit method python
- String join method python
- String zfill method python
- String swapcase method python
- String splitlines method python
- String rsplit method python
- String isspace method python
- what does strip do in python
- Python string lower() method
- Python string lstrip() method
What are various string methods in Python? Various string methods in Python are title(), casefold(), center(), isdigit(), join(). zfill(), swapcase(), splitlines(), rsplit() and isspace() etc.
Let us check out every method with examples.
Contents
- String methods in python
- String title method python
- String casefold method python
- String center method python
- String isdigit method python
- String join method python
- String zfill method python
- String swapcase method python
- String splitlines method python
- String rsplit method python
- String isspace method python
- What does strip do in python
- Python string lower() method
- Python string lstrip() method
String methods in python
Python string methods are very much useful to work with string in Python. Here, we will see all the methods that can be used while working with string in python.
String title method python
In python, we use title() string method which is used to convert the first character in each word to uppercase and rest characters to lowercase in string.
Example:
msg = "welcome to python"
s = msg.title()
print(s)
After writing the above code (string title method python), Ones you will print “ s ” then the output will appear as a “Welcome To Python”. Here, title() method is used to convert the string first character of every word in uppercase.
You can refer to the below screenshot for string title method python.
- How to split a string using regex in python
- How to concatenate strings in python
- How to convert an integer to string in python
- Python String Functions
- Python convert list to string
- Create a tuple in Python
String casefold method python
In python, we can use casefold() method to convert all the uppercase characters to lowercase. Here, casefold method returns the string where all characters are in lowercase.
Example:
msg = "Welcome To Python"
s = msg.casefold()
print(s)
After writing the above code (string casefold method python), Ones you will print “ s ” then the output will appear as a “welcome to python”. Here, casefold() method returns a string where all the characters are in lower case.
You can refer to the below screenshot for string casefold method
String center method python
In python, string center() method will return a centered string of specified size. Here, center() method will align the string in center.
Example:
msg = "Welcome"
s = msg.center(50)
print(s)
After writing the above code (string center method python), Ones you will print “ s ” then the output will appear as a “ Welcome ”. Here, the center() method will center align the string of specified size.
You can refer to the below screenshot for string center method python
String isdigit method python
In python, isdigit() method will check whether the string contains only digit or not. If it contains only digit then it will return true otherwise false.
Example:
msg = "150297"
s = msg.isdigit()
print(s)
After writing the above code (string isdigit method python), Ones you will print “ s ” then the output will appear as a “ True ”. Here, the isdigit() method will return true because the character is a digit.
You can refer to the below screenshot string isdigit method python.
String join method python
In python, the join() method will join all the items into one string by using the “@” character as separator.
Example:
msg = ("Joe", "ricky", "stephen")
s = "@".join(msg)
print(s)
After writing the above code (string join method python), Ones you will print “ s ” then the output will appear as a “ [email protected]@stephen ”. Here, the join() method will join all the tuple into a string by using the “@” character as a separator.
You can refer to the below screenshot for string join method
String zfill method python
In python, zfill() method returns the copy of the string with zero at the beginning of the left side until it is 5 characters.
Example:
msg = "20"
s = msg.zfill(5)
print(s)
After writing the above code (string zfill method python), Ones you will print “ s ” then the output will appear as a “ 00020 ”. Here, the zfill() method will return the copy of the string with the “0” characters from the start of the left side till its specified length.
You can refer to the below screenshot for string zfill method python
String swapcase method python
In python, swapcase() method converts all lowercase characters to uppercase and all uppercase characters to lowercase of a given string.
Example:
msg = "Hello welcome to Python"
s = msg.swapcase()
print(s)
After writing the above code (string swapcase method python), Ones you will print “ s ” then the output will appear as a “ hELL0 WELCOME TO pYTHON ”. Here, the swapcase() method returns the string where all uppercase letters are in lower case and vice versa.
You can refer to the below screenshot for string swapcase method python
String splitlines method python
In python, splitlines() method is used splits the string at line breaks into the list.
Example:
msg = "Welcome to python\nNew to python"
s = msg.splitlines()
print(s)
After writing the above code (string splitlines method python), Ones you will print “ s ” then the output will appear as a “ [‘Welcome to python’, ‘New to python’] ”. Here, the splitlines() method is used to splits the string at line breaks.
You can refer to the below screenshot for string splitlines method python
String rsplit method python
In python, rsplit() method will split the string from right into list with specified seperator.
Example:
msg = "vanilla, chocolate, strawberry"
s = msg.rsplit()
print(s)
After writing the above code (string rsplit method python), Ones you will print “ s ” then the output will appear as a “ [‘vanilla’, ‘chocolate’, ‘strawberry’] ”. Here, the rsplit() method is used to splits the string into list.
You can refer to the below screenshot for string rsplit method python
String isspace method python
In python, isspace() method will return true if there is only whitespace character present in the string otherwise it will return false.
Example:
msg = " "
s = msg.isspace()
print(s)
After writing the above code (string isspace method python), Ones you will print “ s ” then the output will appear as a “ True ”. Here, the isspace() method is used to check the whitespace characters in the string.
You can refer to the below screenshot for string isspace method python
What does strip do in python
The strip() function is built-in function in python. It is used used to remove the white space from the start and end of the string and returns the original string without white space.
Example:
car_name = " BMW X3 "
r = car_name.strip()
print("My fav car is",r)
After writing the above code (what does strip do in python), Ones you will print “ r ” then the output will appear as a “ My fav car is BMW X3 ”. Here, the string.strip() method is used to remove the white space from the start and end.
You can refer to the below screenshot what does strip do in python
Also, If you want to remove the leading and trailing characters from the string then we can use the string.strip(char) method for it.
Example:
car_name = "#####...BMW X3*****CC.."
r = car_name.strip('#*.c')
print("My fav car is",r)
After writing the above code (python string strip() method), Ones you will print “ r ” then the output will appear as a “ My fav car is BMW X3 ”. Here, the string.strip(character) method will remove all the leading and trailing characters from the string.
You can refer to the below screenshot python string strip() method
Python string lower() method
The lower() method is used to converts all uppercase characters of a string into lowercase characters.
Example:
my_string = "Welcome to Python Guides"
my_string = my_string.lower()
print(my_string)
After writing the above code (python string lower() method), Ones you will print “ my_string ” then the output will appear as a “ welcome to python guides ”. Here, the string.lower() method returns a string in lower case.
You can refer to the below screenshot for python string lower() method.
Python string lstrip() method
The lstrip() method returns a new string with leading whitespace and specified characters removed from the left side. If no arguments are passed to lstrip() then it will remove leading spaces from the left side.
my_str = " ***www.pythonguides"
r = my_str.lstrip(" ***www.")
print(r)
After writing the above code (python string lstrip() method), Ones you will print “ r ” then the output will appear as a “ pythonguides ”. Here, lstrip() method is used to remove any leading characters or specified characters from the string.
You can refer to the below screenshot for python string lstrip() method.
You may like the following Python tutorials:
- NameError: name is not defined in Python
- Python check if the variable is an integer
- ValueError: math domain error
- Check if a number is a prime Python
- Python pip is not recognized as an internal or external command
- Sorting algorithms in Python
- Priority queue in Python
In this Python tutorial, we learned about various string methods in python like:
- String methods in python
- String title method python
- String casefold method python
- String center method python
- String isdigit method python
- String join method python
- String zfill method python
- String swapcase method python
- String splitlines method python
- String rsplit method python
- String isspace method python
- what does strip do in python
- Python string lower() method
- Python string lstrip() method
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.