Python 3 string methods with examples

In this Python tutorial, we will discuss various Python 3 string methods with a few examples. These are all built-in methods for various string operations in python. We will discuss the below methods and a few python string programs.

  • 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 string lower() method
  • Python sting istrip() method
  • Python string strip() method
  • String operations in Python Examples
  • How to find length of a string in python
  • Python count characters in a string
  • Python repeat string n times

Python 3 string methods()

Python provides a lot of built-in functions or methods for various string operations in Python. Let us check out the Python string methods with examples.

Read: Slicing string in Python + Examples

Python string title() method

Python title() string method is used to convert the first character in each word to uppercase and the 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.

Python string title() method
String title() method

Python string casefold() method

In python, we can use casefold() string 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

Python string casefold() method
Python string casefold() method

Python string center() method

The python string center() method will return a centered string of specified size. Here, the center() method will align the string in the 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

Python string center() method
Python string center() method

String isdigit() method in 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.

Python string isdigit() method
Python string isdigit() method

Python string join() method

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 in Python.

Python string join() method
Python string join() method

Python string zfill() method

Let us see, how to use the Python string zfill() method.

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.

Python string zfill() method
String zfill() method in python

Python string swapcase() method

Python string 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

Python string swapcase() method
String swapcase() method python

Python string splitlines() method

Python string 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 splitlines method python
Python string splitlines() method

Python string rsplit() method

Python string rsplit() method will split the string from right into list with specified separator.

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

Python string rsplit() method
Python string rsplit() method

Python string isspace() method

Python string 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

Python string isspace() method
Python string isspace() method

Python sting istrip() method

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

Python sting istrip() method
Python sting istrip() method

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 strip() method
Python string strip() method

Python string lower() method

The Python string 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 lower() method
Python string lower() method

Python string lstrip() method

The Python string 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.

Python string lstrip() method
Python string lstrip() method

String operations in Python Examples

Now, let us check out a various examples on string operations in Python.

How to find length of a string in python

  • Using len() which is a built-in function in python, We can use the len() method to get the length of the given string in Python.
  • To find the length of string the built-in function len() methods return the length of a string.
  • The function can take any string value as an input and it will return the integer value as an output.

Example:

  • First of all, We need to take a string value. In the below python code, You can see, I have taken the value as “Python”.
  • Now print the “len(msg)” and it will return the number of item.

Python to find the length of a string

Below represents the python code to find the length of string.

msg = 'Python'
print(len(msg))
  • After writing the above Python code (string length), Ones you will print “len(msg)” then the output will appear as a “6” which is length of the string as an output. Also, you can refer to the below screenshot.
python string length
find length of a string in python

Python count characters in a string

In python, to count the total number of characters in a string we will use for loop to iterate every character in a string.

Example:

my_string = "Hello all"
total = 0
for val in my_string:
total = total + 1
print("Total Number of Characters = ", total)

After writing the above Python code (python count characters in a string), Ones you will print then the output will appear ” Total Number of Characters = 9 “.

Here, for loop will iterate every character in the string and it will increment the total value for each character. Also, you can refer to the below screenshot

Python count characters in a string
Python count characters in a string

This is how to count characters in a string in Python.

Read: Python Check if a variable is a number

Python repeat string n times

To repeat string n times we will use ” * “. The syntax to repeat string ” string * n ” where n as an integer to repeat a string n number of times.

Example:

str_repeat = "Guides" * 5
print(str_repeat)

After writing the above code (python repeat string n times), Ones you will print “str_repeat” then the output will appear as “ GuidesGuidesGuidesGuidesGuides ”. Here, the value of n is 5 and the string will repeat n times.

You can refer to the below screenshot python repeat string n times

Python repeat string n times
Python repeat string n times

This is how to repeat string n times in Python.

Python string upper() method

You can use the Python string upper() method to return the string in the upper case.

Example:

x = "pythonguides"

print(x.upper())

Output will be

PYTHONGUIDES
Python string methods
Python String Functions

Python string lower() method

You can use the Python string lower() method to return the string in lower case.

Example:

x = "PYTHONGUIDES"

print(x.lower())

Output will be

pythonguides
Builtin methods in python string
Python String Functions

Python string strip() method

We can use the strip() method in Python to remove the white space if it presents on the starting of the string.

Example:

x = "   PythonGuides"

print(x.strip())

Now see the output here

Python string lower method
Python String strip() Functions

Python string replace() method

You can use replace() method in Python to replace a character inside a string with another character or to replace a string with another string.

Example:

x = "I love Python"

print(x.replace("love","like")

Output will be

I like Python
Replace method in python string
Python String replace() Functions

split() method in Python

We can use the Python split() method to split the string into substrings.

Example:

x = "python, guides"

print(x.split(","))

Output will be

['python', ' guides']
How to use split method in python string
split() method in python string

Python count() method

We can use the count() method in Python to count the number of characters used inside a string.

Example:

x = "I love Python"

print(x.count('o'))

So we are asking how much time “o” is present inside the above string. The output will be 2.

count function in python string
count() in python string

Python string center() method

We can use the Python string center() method to use to get the centered string.

Example

val = "PythonGuides"

y = val.center(20,"O")

print(y)

Output will be

OOOOPythonGuidesOOOO
How to use center function in python
center() in python

You may like the following Python tutorials:

In this Python tutorial, we learned about various python 3 string methods() with examples.

  • Python 3 string methods()
  • Python string title() method
  • Python string casefold() method
  • Python string center() method
  • String isdigit() method in Python
  • 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 sting istrip() method
  • Python string lower() method
  • Python string lstrip() method
  • String operations in Python Examples
  • How to find length of a string in python
  • Python count characters in a string
  • Python repeat string n times