Python String Methods Tutorial

Strings(sequence of characters) are widely used in programming and Python. In Python strings are treated as built-in type or str objects. These objects are immutable sequences of characters.

Create Strings

String literals in python can be represented in many ways(single or double quoted):

str1 = "This is string 1"
str2 = 'This is string 2'

Both ways are available from the official documentation. The difference is more about coding style and personal preferences.
"Strings are immutable" can be explained by this simple code:

str1 = "This is string 1"
print(id(str1))
str1 = "This is string 2"
print(id(str1))
print(str1)

result:

19901584
19901440
This is string 2

As you can see the variable str1 has new value but also the object id returned by method(id) is different. So new object was created for this change. If you try to set the old value you will get the old object id.

Multiline Strings in Python

Python support native multiline strings in two ways:

multiLineStr1 = '''Line 1 
Line 2
Line 3'''
multiLineStr2 = """Line 1 
Line 2
Line 3"""
print(multiLineStr1)

result:

Line 1 
Line 2
Line 3

Some IDEs can treat multilistrings (like PyCharm) in this way:

str1 = "This is string" \
      " 1"

Substrings in Python

Substrings are strings which are create by extracting character sequence from another string. In Python substrings are created by indexes. You have many ways to define your start, end and step for the substring:

str1 = "This is string 1"

print(str1[0]) # Return first character - index 0
print(str1[-1]) # Return last character - index -1
print(str1[0:4]) # Return character sequence - from index 0 to 4
print(str1[8:15:2]) # Return character sequence - from index 8 to 15 using step 2
print(str1[5:]) # return from character 5 up to the end
print(str1[:10]) # return since the beginning up to character 10

result:

T
1
This
srn 
is string 1
This is st

If you use comma for index in Python you will get error:

print(str1[3,6])

the error is:

TypeError: string indices must be integers

String subtraction in Python

In some languages you can apply operator minus - to strings. This is not the case in Python. You need to implement a method for this purpose. Below you can find two ways of string subtraction in python:

print("line 1, line 2, line 3".replace("line 2, ", ""))

def minus(str1, str2):
    return "".join(str1.rsplit(str2))

print(minus("line 1, line 2, line 3", "line 2, "))

result:

line 1, line 3
line 1, line 3

String append in Python

Another often operation with strings is joining or appending strings. This can done again in several ways:

print("line 1, line 2, line 3" + ", line 4")
print("".join(("line 1, line 2, line 3", ", line 4")))

result:

line 1, line 2, line 3, line 4
line 1, line 2, line 3, line 4

Which way you are going to choose depends on several factors: number of the strings, length and performance.

Note: method join can be a bit confusing for beginners. For example:

print("line 1, line 2, line 3".join(", line 4"))
print("<>".join(["123"]))
print("<>".join(["123", "456"]))
print("<>".join("123"))

result in:

,line 1, line 2, line 3 line 1, line 2, line 3lline 1, line 2, line 3iline 1, line 2, line 3nline 1, line 2, line 3eline 1, line 2, line 3 line 1, line 2, line 34
123
123<>456
1<>2<>3    

So in other words if you want to join several strings you need to pass them as list of strings. The method is called on another string which is used as separator for the string join.

Python String Useful Methods

In this section are listed some of the most frequently used methods of string Object. There is explanation and example to most of them:

Join, Replace and Split

Manipulating strings is done mainly by 3 methods:

  • str.join(seq): joins list of strings based on the separator provided.
  • replace(str1, str2): remove a string from another string. If the string is not matched the initial string is not changed.
  • split('separator'): splits the string based on the separator provided. Default separator(nothing is set as parameter) is space.
#join
print("".join(["line 1, line 2, line 3"]))

# String replace
print("line 1, line 2, line 3".replace("line 2, ", ""))
print("line 1, line 2, line 3".replace("line 4, ", ""))

# String split
print("line 1, line 2, line 3".split())
print("line 1, line 2, line 3".split(","))

result:

line 1, line 2, line 3
line 1, line 3
line 1, line 2, line 3
['line', '1,', 'line', '2,', 'line', '3']
['line 1', ' line 2', ' line 3']    

Find and Strip

Searching and removing of strings in other strings can be done with following methods:

  • find('str'): Returns the first index of the found substring (-1 if the substring is missing).
  • rfind('str'): Returns the last index of the found substring (-1 if the substring is missing).
  • rstrip(): This method returns the string after removing all the characters from the end of the string.
  • lstrip(): This method returns the string after removing all the characters from the beginning of the string.
# String find
print("line 1, line 2, line 3".rfind('line'))
print("line 1, line 2, line 3".find('line'))

# String strip
print("line 1, line 2, line 3".rstrip('line 3'))
print("line 1, line 2, line 3".strip('line'))
print("line 1, line 2, line 3".lstrip('line 1'))

result:

16
0
line 1, line 2,
 1, line 2, line 3
, line 2, line 3

Count and Len

Two methods help to retrieve some statistical information about your strings:

  • count('str'): return count of occurrence of a given string in another string.
  • len('str'): return the length of the string.
print("line 1, line 2, line 3".count('line'))
print(len("line 1, line 2, line 3"))

result:

3
22    

Other methods

This methods will help you when you need to change the case of the string characters or check is it lower / upper or a digit:

  • lower(): converts every character of the string into lower case.
  • upper(): converts every character of string into lowercase
  • capitalize(): Convert only the first character of a string to upper case
  • islower(): returns True if all the characters in the string are in lowercase. Otherwise return False (even if only 1 character is in upper case).
  • isupper(): returns True if all the characters in the string are in uppercase. Otherwise return False (even if only 1 character is in lower case).
  • isdigit(): Check if the string represents a number and return True if yes
print("line 1, line 2, line 3".upper())
print("line 1, line 2, line 3".capitalize())

for i in "line 1, line 2, line 3".split(', '):
    print(i.capitalize())
    
print("line 1, line 2, line 3".isdigit())
print("3".isdigit())    

result:

LINE 1, LINE 2, LINE 3
Line 1, line 2, line 3
Line 1
Line 2
Line 3
False
True

Related Article