Strings#

Strings in Python are an array of bytes representing Unicode characters and the length can be a single character or a sequence of characters. The elements of a string have no data type and can be accessed using the index operator. In Python strings are surrounds by either single quotation marks, or double quotation marks. PEP 8 describes no standard on how to use single or double-quotes:

Note

In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.

For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.

Some tools like Black have a preference to have all strings and comments in double quotes, but both ways are correct.

The basics about strings#

The most basic form of a litereal string is one that is given directly to print().

Example: Print a litereal string.#
1 #!/usr/bin/env python3
2
3 def main():
4     print("Hello World.")
5
6
7 if __name__ == "__main__":
8     main()
Output: Print a litereal string.#
1 Hello World.

The second form is to assign a variable to a string and can be used by print() as a reference to the string.

Example: Print a string referenced by a variable.#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello World."
5     print(phrase)
6
7
8 if __name__ == "__main__":
9     main()
Outpur: Print a string referenced by a variable.#
1 Hello World.

As seen in chapter Variables and Types, variables can be joined with the +-sign and a string can also be concatenated with a variable.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase_one = "Hello World."
 5     phrase_two = "And Goodbye."
 6     # Concatenate two variables
 7     print(phrase_one + phrase_two)
 8     # Concatenate two variables with a string
 9     print(phrase_one + " " + phrase_two)
10
11
12 if __name__ == "__main__":
13     main()
Output#
1 Hello World.And Goodbye.
2 Hello World. And Goodbye.

Strings can also be a multiline string with a newline character as part of the value. Python does take the indentation of a multiline string not into account and will the indentation will be part of the string. On Stack Overflow in question 2504411 possible solutions to work around this issue are discusses.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = """Hello World.
 5     And Goodbye."""
 6     print(phrase)
 7
 8
 9 if __name__ == "__main__":
10     main()
Output#
1 Hello World.
2     And Goodbye.

Strings are arrays#

Strings are like in other languages arrays and can be address in that way. The working of arrays is described in Arrays, but for now we read the second element of the array and print it.

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello World."
5     print(phrase[1])
6
7
8 if __name__ == "__main__":
9     main()
Output#
1 e

As a strings is an array you can easily loop over all elements and get every element separately.

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     for x in "Hello World.":
5         print(x)
6
7
8 if __name__ == "__main__":
9     main()
Output#
 1 H
 2 e
 3 l
 4 l
 5 o
 6
 7 W
 8 o
 9 r
10 l
11 d
12 .

String length#

The built-in function len() return the length of an object and used the internal method __len__() of the object to determine the length.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = "Hello World."
 5     # Using the built-in version
 6     print(len(phrase))
 7     # Using the internal method of an object
 8     print(phrase.__len__())
 9
10
11 if __name__ == "__main__":
12     main()
Output#
1 12
2 12

Checking a string#

With the in operator you can check if a string is in another string. On line 6 we check if the string "Hello" is in the string "Hello World." and the result is True. The second check on line 8 is checking if the string "Hello" isn’t in the string "Hello World." and the result is No, Hello World.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = "Hello World."
 5
 6     print("Hello" in phrase)
 7
 8     if "Hello" not in phrase:
 9         print("Yes, Hello World.")
10     else:
11         print("No, Hello World.")
12
13
14 if __name__ == "__main__":
15     main()
Output#
1 True
2 No, Hello World.

Slicing strings#

Example: Slicing strings#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = "Hello World."
 5     print(phrase[2:5])
 6     print(phrase[:5])
 7     print(phrase[2:])
 8     print(phrase[-5:-2])
 9
10
11 if __name__ == "__main__":
12     main()
Output: Slicing strings#
1 llo
2 Hello
3 llo World.
4 orl

String methods#

Convert a string to upper or lower case#

With String methods upper() and lower() a copy of the string is returned after all chareacters are converted to uppercase or lowercase.

Example: Converting a string to both upper- and lowercase#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = "Hello World."
 5     print(phrase.upper())
 6     print(phrase.lower())
 7
 8
 9 if __name__ == "__main__":
10     main()
Output: Converting a string to both upper- and lowercase#
1 HELLO WORLD.
2 hello world.

Note

Both methods follow the Unicode Standard and may for example still contain a lowercase character after calling upper() if that is defined in the Unicode Standard.

Trim a string#

The method strip() removes by default whitespace character from the string on both sides. With lstrip() or rstrip() the string is only being trimmed on the left or ride side.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = " Hello World. "
 5     print(phrase)
 6     print(phrase.strip())
 7     print(phrase.lstrip())
 8     print(phrase.rstrip())
 9
10
11 if __name__ == "__main__":
12     main()
Output#
1  Hello World.
2 Hello World.
3 Hello World.
4  Hello World.

The method strip() by default trims whitespace characters, but can also use other characters to trim a string.

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello World."
5     print(phrase.strip("HeldW."))
6
7
8 if __name__ == "__main__":
9     main()
Output#
1 o Wor

Replacing a string#

The method replace() replaces a string with another string.

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello World."
5     print(phrase.replace("W", "w"))
6
7
8 if __name__ == "__main__":
9     main()
Output#
1 Hello world.

Split and join#

The method split() splits a string into a list of strings.

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello World."
5     print(phrase.split(" "))
6
7
8 if __name__ == "__main__":
9     main()
Output#
1 ['Hello', 'World.']

The method join() joins a list of strings into a string.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrases = ["Hello", "World."]
 5     separator = " "
 6     print(separator.join(phrases))
 7
 8
 9 if __name__ == "__main__":
10     main()
Output#
1 Hello World.

The method join() can also be used to join a list of strings into a string with a separator.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrases = {"wordOne": "Hello", "wordTwo": "World."}
 5     separator = "-"
 6     print(separator.join(phrases))
 7
 8
 9 if __name__ == "__main__":
10     main()
Output#
1 wordOne-wordTwo

Formatting strings#

The method format() formats a string with placeholders.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     name = "World"
 5     phrase = "Hello {}."
 6     print(phrase.format(name))
 7
 8
 9 if __name__ == "__main__":
10     main()
Output#
1 Hello World.

The method format() can also be used to format a string with a list for the placeholders.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     name_one = "Jack"
 5     name_two = "John"
 6     phrase = "Hello {} and {}."
 7     print(phrase.format(name_one, name_two))
 8
 9
10 if __name__ == "__main__":
11     main()
Output#
1 Hello Jack and John.

The placeholders for the method format() can also be used as keys in a dictionary.

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     name_one = "Jack"
 5     name_two = "John"
 6     phrase = "Hello {1} and {0}."
 7     print(phrase.format(name_one, name_two))
 8
 9
10 if __name__ == "__main__":
11     main()
Output#
1 Hello John and Jack.

Escape characters#

Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello "World"."
5     print(phrase)
6
7
8 if __name__ == "__main__":
9     main()
Output#
1   File "/workspaces/mastering-python/example.py", line 4
2     phrase = "Hello "World"."
3                      ^
4 SyntaxError: invalid syntax
Example#
1 #!/usr/bin/env python3
2
3 def main():
4     phrase = "Hello \"World\"."
5     print(phrase)
6
7
8 if __name__ == "__main__":
9     main()
Output#
1 Hello "World".

Code

Result

\’

Single Quote

\\

Backslash

\n

New Line

\r

Carriage Return

\t

Tab

\b

Backspace

\f

Form Feed

\ooo

Octal value

\xhh

Hex value

Example#
 1 #!/usr/bin/env python3
 2
 3 def main():
 4     phrase = "Hello\n \"World\"."
 5     print(phrase)
 6     phrase = 'Hello\n \"World\".'
 7     print(phrase)
 8
 9
10 if __name__ == "__main__":
11     main()
Output#
1 Hello
2  "World".