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:
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.