STRINGS · 8 MIN
Strings
A string is an ordered, unchangeable sequence of characters.
PYRA explains
- Index with s[0]; negative indexes count from the end: s[-1] is the last character.
- Slicing s[start:stop] returns a new string; stop is excluded.
- Useful methods: upper(), lower(), strip(), split(), replace(), and f-strings for formatting.
Real world: Cleaning user names typed into a sign-up form: trimming spaces and fixing capitals.
Code
s = " Cubix World "
t = s.strip()
print(t[0], t[-1], t[:5].upper())
print(f"{t} has {len(t)} chars")Output ›
C d CUBIX
Cubix World has 11 charsTry it in the online compiler →Checkpoint quiz
"python"[1:4] is…
Can you do s[0] = "X" on a string?