Python String Function Examples

When you operate on a python string, it will create a new python string based on the original string. The original string value will not be changed. This article will list some python string operation example.

1. Split Python String.

The string’s split() function will split a python string to a words list.

>>> str_1 = 'python is a good programming'
# the python string split() function will split the string and return a string word list.
>>> words_list = str_1.split()
>>> words_list
['python', 'is', 'a', 'good', 'programming']
# but the original string will not be changed.
>>> str_1
'python is a good programming'

2. Join Python String.

The join() function will join a python list to a string use provided separator. This function will return a new string object.

# join the string list elements use different separator.
>>> " ".join(words_list)
'python is a good programming'

>>> ",".join(words_list)
'python,is,a,good,programming'

>>> " : ".join(words_list)
'python : is : a : good : programming'
# the original list is not changed. 
>>> words_list
['python', 'is', 'a', 'good', 'programming']

3. String Supper Function.

This method will change all string character to upper case.

>>> str_1 = 'python is a good programming'
>>> str_2 = str_1.upper()

>>> str_2
'PYTHON IS A GOOD PROGRAMMING'

>>> str_1
'python is a good programming'

# check whether the string is uppercase or not.
>>> str_2.isupper()
True

>>> str_1.isupper()
False

4. String Capitalize Function.

Uppercase the first character of the string.

>>> str_1 = 'python is a good programming'
>>> str_2 = str_1.capitalize()
>>> str_2
'Python is a good programming'

5. String Title Function.

Make each word’s first character uppercase in the string.

>>> str_1 = 'python is a good programming'
>>> str_2 = str_1.title()
>>> 
>>> str_1
'python is a good programming'
>>> 
>>> str_2
'Python Is A Good Programming'

6. String Count Function.

Return the provided string exist count number in original string.

>>> str_1 = 'python is a good programming'
>>>
# get provided string count in the source string. 
>>> str_1.count('g')
3

7. String Find Function.

Get the provided string’s index in the original string.

>>> str_1 = 'python is a good programming'

>>> str_1.find('yt')
1
# if the target string dose not exist, then return -1.
>>> str_1.find('java')
-1

8. String startswith & endswith Function.

>>> str_1 = 'python is a good programming'

>>> str_1.startswith('python')
True

>>> str_1.startswith('Python')
False

>>> str_1.endswith('python')
False

9. String Replace Function.

>>> str_1 = 'python is a good programming'
>>> 

# replace word 'python' with 'java'
>>> str_2 = str_1.replace('python', 'java')
>>> 
>>> str_2
'java is a good programming'
>>> 
>>> str_1
'python is a good programming'

10. Remove String Beginning Or Ending White Space.

Python string’s strip(), lstrip() and rstrip() function will remove string’s leading or tailing whitespace.

>>> str_1 = '  python is a good programming   '
>>> 
# remove both beginning and endding white space.
>>> str_2 = str_1.strip()
>>> 
>>> str_2
'python is a good programming'
>>> 
>>> str_1 = '  python is a good programming   '
# remove beginning white space.
>>> str_2 = str_1.lstrip()
>>> 
>>> str_2
'python is a good programming   '
# remove endding white space.
>>> str_2 = str_1.rstrip()
>>> 
>>> str_2
'  python is a good programming

11. Get String Length.

The len() method will return the string character size. But it is a system built-in method.

>>> str_1 = ' python '
>>> 

# the string length contains the white space in the original string.
>>> len(str_1)
8

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.