Python String Functions

Python String Functions Tutorial:

In this Excellent Python Training series we learned about Python DateTime in detail.

Here, in this tutorial, we will explore the various string functions that are available in Python.

We will also discuss few other concepts like creating and deleting a file with examples.

Watch the VIDEO Tutorials

Various String Functions in Python:

Python String Functions

Let's take a look at the various string functions in Python.

#1) String Replace

This method is used to replace the string, which accepts two arguments.

Example:

lang = “Hello Selenium”
print(lang.replace(“Selenium”, “Python”)) 

Output:

Hello Python

Python - string_replace

Output:

Python string - output

#2) String Reverse

This method is used to reverse a given string

Example:

 
lang = “Python”
print(‘’.join(reversed(lang))) 

Output:

nohtyP

Python string reverse

Output:

Python string reverse output

#3) String Join

This method returns the string concatenated with the elements of iterable.

Example:

 
s1 = “ABC”
s2 = “123”
print(s1.join(s2)) 

Output:

1ABC2ABC3

Python string join

Output:

Python string join output

#4) String Split

This method is used to split the string based on the user arguments

Example:

 
text = “Welcome to Python”
print(text.split()) 

Output:

[‘Welcome’, ‘to’, ‘Python’]

Python string split

Output:

string_split_output

#5) String Length

This method returns the length of the String.

Example:

 
text = “Python”
print(len(text)) 

Output:

6

Python string length

Output:

Python string length output

#6) String Compare

This method is used to compare two strings.

Example:

 
s1 = “Python”
s2 = “Python”
if(s1 == s2):
      print(“Both strings are equal”) 

Output:

Both strings are equal

Python string compare

Output:

Python string compare output

#7) String Lowercase

This method is used to convert the uppercase to lowercase.

Example:

text = “PYTHON”
print(text.lower()) 

Output:

python

Python string lowercase

Output:

Python string lowercase output

Various Python String Functions were explained in detail with classic examples here in this tutorial.

Our upcoming tutorial will brief you more about Python File Handling in detail!!

PREV Tutorial | NEXT Tutorial