Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Built-In String Functions

Give Feedback

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')
>>> ord(' ')
>>> ord('#')
>>> ord('€')
>>> ord('∑')
>>> ord('🥓')

Here are some examples of chr() in use:

>>> chr('97')
>>> chr(35)
>>> chr(32)
' '
>>> chr(129363)

Here are some examples of len() in use:

>>> s = 'I am a string'
>>> len(s)

>>> s = ''
>>> len(s)

Here are some examples of str() in use:

>>> str(49.2)
>>> str(3 + 29)
>>> a = str(3 + 29)
>>> a
>>> type(a)
<class 'str'>

To learn more, you can check out Basic Data Types in Python. Here are some resources on characters and encoding:

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.

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.