8 minute read

Strings are one of the basic types that are built into the Python interpreter.1 You can create strings (str) by enclosing a sequence of characters within a pair of single or double quotes.

Python does not support character type. Characters are treated as strings of length 1 or substrings.

# created a string variable called emre
emre = ""

# Get the character at position 0
# Output: e

# Get the substring character from position 4 to 5
# Output: .

Python provides a rich set of operators and methods for working with strings. We will explain the most important ones in this article.

String Operators

Concatenation and Repetition

Strings are immutable in Python, they cannot be modified after being created. Using concatenation (+) and repetition (*) operators returns a new string.

# created some strings
str1 = "emre"
str2 = "."
str3 = "me"

# concatenation of strings
# Output:
print(str1 + str2 + str3)

# repetition operator
# Output: emreemreemre
print(str1 * 3)

String Membership Test

It is possible to test if a substring exists in a string or not, with using in and not in operators.

# created a string variable called emre
emre = ""

# Output: True
print("me" in emre)

# Output: False
print("xyz" in emre)

# Output: True
print("e.m.r.e" not in emre)

Raw String

Raw string operator (r) is useful for the cases where you need to print the actual meaning of escape character (\) such as C:\Program Files.

# Output: ""

# Output: \"\"

String Methods

.upper() and .lower()

The functions .upper() and .lower() will return a string with all the letters from the original string converted to upper or lower case letters.

# created a new string
str1 = "Emre.Me"

# Output:

# Output: EMRE.ME

.title, .swapcase() and .capitalize()

.title() changes the first letter of each word to upper case and all other letters to lower case.

.swapcase() method changes all upper case letters to lower case and vice versa.

.capitalize() method works like .title() but it only changes the first letter of first word to upper case, and makes the rest lower case.

# create a new string
str1 = "EmRe Dot mE"

# Output: Emre Dot Me

# Output: eMrE dOT Me

# Output: Emre dot me

Boolean Methods

These string methods evaluates the type of the string.

Method True If
.isalnum() String consists of only alphanumeric characters
.isalpha() String consists of only alphabetic characters
.isnumeric() String consists of only numeric characters
.islower() String’s alphabetic characters are all lower case
.isupper() String’s alphabetic characters are all upper case
.isspace() String consists of only whitespace characters
.istitle() String is in title case
# create a new string
str1 = "Python4Beginners"

# Output: True

# create a new string
str2 = "PythonForBeginners"

# Output: True

# create a new string
str3 = "2019"

# Output: True

# create a new string
str4 = "emre"

# Output: True

# create a new string
str5 = "EMRE"

# Output: True

# create a new string
str6 = " "

# Output: True

# create a new string
str7 = "Emre Bolat"

# Output: True

String Length

Built-in string method .len() returns the number of characters in a string.

# create a new string
emre = ""

# Output: 7

.join(), .split() and .replace()

.join(), .split() and .replace() are very useful methods for manipulating strings in Python.

# create a new string
str1 = ""
str2 = "emre"
str3 = "me"

# Output: e m r e . m e
print(" ".join(str1))

# Output:
print(".".join([str2, str3]))

# Output: ['emre', 'me']

# Output: emre-me
print(str1.replace(".", "-"))

Additional String Methods

.strip(), .rstrip() and .lstrip()

Returns a copy of the string with the leading .lstrip() and trailing .rstrip() whitespace removed. Method .strip() removes both.

# create a new string
str1 = "   "

# Output: "   "

# Output: ""

# Output: ""

.find() and .rfind()

.find() method returns the index of the first found occurrence of the given subsequence searching from left-to-right. .rfind() method searches right-to-left.

# create a new string
str1 = ""

# Output: 1

# Output: 5


.count() method counts the number of occurrences of one string within another string.

# create a new string
str1 = ""

# Output: 3

With using three parameters .count(substring, left, right), the count is performed within the slice [left:right].

# create a new string
str1 = ""

# Output: 1
print(str1.count("e", 1, 6))

Output is 1 because, by writing .count("e", 1, 6), we actually count the number of e characters in [1:6] split of, which is mre.m.


  1. The Python Standard Library, Text Sequence Type - str