Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Strings and Character Data in Python
In the tutorial on Basic Data Types in Python, you learned how to define strings: objects that contain sequences of character data. Processing character data is integral to programming. It is a rare application that doesn’t need to manipulate strings at least to some extent.
Here’s what you’ll learn in this tutorial: Python provides a rich set of operators, functions, and methods for working with strings. When you are finished with this tutorial, you will know how to access and extract portions of strings, and also be familiar with the methods that are available to manipulate and modify string data.
You will also be introduced to two other Python objects used to represent raw byte data, the bytes and bytearray types.
Take the Quiz: Test your knowledge with our interactive “Python Strings and Character Data” quiz. Upon completion you will receive a score so you can track your learning progress over time:
String Manipulation
The sections below highlight the operators, methods, and functions that are available for working with strings.
String Operators
You have already seen the operators + and * applied to numeric operands in the tutorial on Operators and Expressions in Python. These two operators can be applied to strings as well.
The + Operator
The + operator concatenates strings. It returns a string consisting of the operands joined together, as shown here:
>>> s = 'foo'
>>> t = 'bar'
>>> u = 'baz'
>>> s + t
'foobar'
>>> s + t + u
'foobarbaz'
>>> print('Go team' + '!!!')
Go team!!!
The * Operator
The * operator creates multiple copies of a string. If s is a string and n is an integer, either of the following expressions returns a string consisting of n concatenated copies of s:
s * n
n * s
Here are examples of both forms:
>>> s = 'foo.'
>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
The multiplier operand n must be an integer. You’d think it would be required to be a positive integer, but amusingly, it can be zero or negative, in which case the result is an empty string:
>>> 'foo' * -8
''
If you were to create a string variable and initialize it to the empty string by assigning it the value 'foo' * -8, anyone would rightly think you were a bit daft. But it would work.
The in Operator
Python also provides a membership operator that can be used with strings. The in operator returns True if the first operand is contained within the second, and False otherwise:
>>> s = 'foo'
>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
There is also a not in operator, which does the opposite:
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
Built-in String Functions
As you saw in the tutorial on Basic Data Types in Python, Python provides many functions that are built-in to the interpreter and always available. Here are a few that work with strings:
| Function | Description |
|---|---|
chr() |
Converts an integer to a character |
ord() |
Converts a character to an integer |
len() |
Returns the length of a string |
str() |
Returns a string representation of an object |
These are explored more fully below.
ord(c)
Returns an integer value for the given character.
At the most basic level, computers store all information as numbers. To represent character data, a translation scheme is used which maps each character to its representative number.
The simplest scheme in common use is called ASCII. It covers the common Latin characters you are probably most accustomed to working with. For these characters, ord(c) returns the ASCII value for character c:
>>> ord('a')
97
>>> ord('#')
35
ASCII is fine as far as it goes. But there are many different languages in use in the world and countless symbols and glyphs that appear in digital media. The full set of characters that potentially may need to be represented in computer code far surpasses the ordinary Latin letters, numbers, and symbols you usually see.
Unicode is an ambitious standard that attempts to provide a numeric code for every possible character, in every possible language, on every possible platform. Python 3 supports Unicode extensively, including allowing Unicode characters within strings.
For More Information: See Unicode & Character Encodings in Python: A Painless Guide and Python’s Unicode Support in the Python documentation.
As long as you stay in the domain of the common characters, there is little practical difference between ASCII and Unicode. But the ord() function will return numeric values for Unicode characters as well:
>>> ord('€')
8364
>>> ord('∑')
8721
chr(n)
Returns a character value for the given integer.
chr() does the reverse of ord(). Given a numeric value n, chr(n) returns a string representing the character that corresponds to n:
>>> chr(97)
'a'
>>> chr(35)
'#'
chr() handles Unicode characters as well:
>>> chr(8364)
'€'
>>> chr(8721)
'∑'
len(s)
Returns the length of a string.
With len(), you can check Python string length. len(s) returns the number of characters in s:
>>> s = 'I am a string.'
>>> len(s)
14
str(obj)
Returns a string representation of an object.
Virtually any object in Python can be rendered as a string. str(obj) returns the string representation of object obj: