str Built-in Functions¶
Several built-in functions work naturally with strings.
These functions help inspect, transform, and analyze text.
Common examples include:
len()str()sorted()reversed()enumerate()ord()chr()
flowchart TD
A[Built-ins for strings]
A --> B[len]
A --> C[sorted]
A --> D[reversed]
A --> E[enumerate]
A --> F[ord/chr]
````
---
## 1. len()
Returns the number of characters in a string.
```python
text = "Python"
print(len(text))
Output:
6
2. str()¶
Converts values to strings.
x = 42
print(str(x))
Output:
42
3. sorted()¶
Returns a sorted list of characters.
text = "cab"
print(sorted(text))
Output:
['a', 'b', 'c']
4. reversed()¶
Returns characters in reverse order.
text = "abc"
print(list(reversed(text)))
Output:
['c', 'b', 'a']
5. enumerate()¶
Pairs indexes with characters.
for i, ch in enumerate("cat"):
print(i, ch)
Output:
0 c
1 a
2 t
6. ord() and chr()¶
ord() converts a character to its Unicode code point.
chr() converts a code point back to a character.
print(ord("A"))
print(chr(65))
Output:
65
A
7. Worked Examples¶
Example 1: count letters using len¶
word = "banana"
print(len(word))
Example 2: alphabetical characters¶
print(sorted("python"))
Example 3: character code¶
print(ord("z"))
8. Summary¶
Key ideas:
- many built-ins work naturally with strings
len()measures stringssorted()andreversed()operate on charactersenumerate()pairs indexes with charactersord()andchr()connect characters to Unicode integers
Built-in functions complement string methods and expand what can be done with text.