Built-In String Functions
Python provides many functions that are built into the interpreter and always available. In this lesson, you’ll see a few that work with strings and character data:
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 |
Here are some examples of ord()
in use:
>>> ord('a')
97
>>> ord(' ')
32
>>> ord('#')
35
>>> ord('€')
8364
>>> ord('∑')
8721
>>> ord('🥓')
129363
Here are some examples of chr()
in use:
>>> chr('97')
'a'
>>> chr(35)
'#'
>>> chr(32)
' '
>>> chr(129363)
'🥓'
Here are some examples of len()
in use:
>>> s = 'I am a string'
>>> len(s)
13
>>> s = ''
>>> len(s)
0
Here are some examples of str()
in use:
>>> str(49.2)
'49.2'
>>> str(3 + 29)
'32'
>>> a = str(3 + 29)
>>> a
'32'
>>> type(a)
<class 'str'>
To learn more, you can check out Basic Data Types in Python. Here are some resources on characters and encoding:
00:00 In this video, I’m going to show you a few of Python’s built-in string functions.
00:05
What I mean by built-in string functions is there’s a few functions that are built into the Python interpreter that will work with strings. The first two functions, chr()
and ord()
, are related in some ways.
00:17
First up is chr()
, which converts an integer value into a character.
00:23
We’ll talk about what the values are and kind of go back and forth between that and the second one, which is ord()
, which stands for ordinal. In that case, it takes a character, any single character, and turns that into an integer.
00:36
The next, len()
, or length, will return the length of the string. It’ll give a value, again in an integer. And then the last, str()
, will take what’s put inside the parentheses and return a string representation of that object. Now let’s practice each one. First off, I’ll show you ord()
. In bpython, again, it’s kind of nice—it’ll actually tell us a little bit about the function.
01:02
So ord()
returns the Unicode code point for a one-character string. So any single character that I put in here, such as the letter 'a'
, is going to return a numeric value—the Unicode code point for that, which is 97
. Okay.
01:19 Unicode is a very ambitious standard. It builds on top of something you might’ve already seen before, ASCII, which is one of the initial standards to kind of get going turning characters into the bits and bytes that are inside the computer.
01:35
ASCII used seven bits, which allowed for 128 values for representing the characters. Unicode builds on top of that. It’s actually very ambitious—it actually goes all the way into providing numeric codes for every possible character in every possible language and tries to be on every possible platform. This tutorial is not going to go into any real depth on Unicode, but I provide links below this video for additional articles that are written on Real Python, and then also some additional links out there for the standards itself. Not to get too far into the weeds, but even something as simple as a space (' '
) has a numeric value.
02:17
Or, say, the number sign ('#'
).
02:20
So, all these characters are being turned into numbers with the ord()
function, which again, stands for ordinal. And even characters that are a little hard to type right on your keyboard, like on this Mac if I wanted to create the Euro symbol ('€'
), then I’d have to hold Shift + Option and press the number 2, that’s 8364
.
02:42
Or maybe the sign for Sigma ('∑'
), which is Option + W. Great. So, all of these have their numeric code. Another ambitious thing that Unicode’s doing now is it actually works with emojis, so if I had an emoji character such as the emoji for bacon, that has a character number for it also. So, kind of cool.
03:04
So, what does chr()
do? Well, it’s the reverse. So with chr()
it returns a Unicode string of one character with the ordinal i
, as long as i
is within this range. In this case, like if I wanted to create the letter 'a'
, I just type 97
within the parentheses and then it’ll return that string—well, in this case, a single-character string. So, same thing here.
03:29
If I wanted 35
, I’d get the number sign ('#'
).
03:35
If I want a space (' '
), or even if I wanted that emoji.
03:42
Kind of neat. So chr()
and ord()
are reversal functions of each other.
03:49
So, what about length, len()
? It’s going to return the number of items in a container. Well, okay. Here, the container you’re going to put in is a string, something as simple as 'I am a string'
.
04:01
This container s
—what’s the length of it?
04:06
That would have 13
characters in it, if you were to count them out. So it simply returns the number of characters in the string as an integer.
04:14
If you had a really simple string, basically an empty string, it would be 0
. So, what about str()
?
04:23
str()
is going to create a new string object from the given object. For this example, we’re not going to focus on encoding. It has a lot to do with that same subject of Unicode and lots of ways that data can be turned into text, so I’ll include links inside the text below this video.
04:42
Let’s say you had a float, 49.2
, but you need to represent that in a string. str()
will convert it into a string for you. So str()
can also be the result of some arithmetic.
04:58
If you take 3 + 29
, str()
would take that sum and return it as a string value of '32'
. To emphasize and clarify what I’m talking about, make a variable a
, and we take 3 + 29
. So we’re returning a string of 3 + 29
.
05:16
So what is a
? a
is '32'
. If we look at the type()
of a
, it is a string. It’s not an integer. Great! Next up, string indexing.
Chris Bailey RP Team on Dec. 16, 2019
Hi @294daniel, The terminal tool I’m using is a REPL replacement called BPython. I have links for more info on it in the previous lesson to this one, in the text below it. realpython.com/lessons/string-operators/
Alain Rouleau on July 29, 2020
Thanks for covering the chr ()
and ord()
functions. You don’t see those two functions very often. But extremely handy. I was playing around with them. So, you can do something like this:
>>> celcius = chr (8451)
>>> fahrenheit = chr (8457)
>>> print(f'28{celcius} equals 82{fahrenheit}')
28℃ equals 82℉
>>> for ordinal in range(224, 230):
... print(chr (ordinal), end=' ')
>>>
à á â ã ä å
Note: For whatever reason, Markdown preview doesn’t like the chr () function inside the comment or code block. So, that’s why I put a space after “chr”.
Become a Member to join the conversation.
Daniel on Dec. 15, 2019
Hi! I’m interested in knowing which python terminal are you using in this video series. It’s cool the popup text box with the usage of every command you write in the terminal. Thanks.