Python Foundations
Working with Strings
A string is a sequence of characters, and each character has a position called an index. Indexing starts at 0:
word = "Python"
print(word[0]) # P
print(word[1]) # yNegative indexes count from the end, so word[-1] is the last character (n).
Slicing
Grab a range with word[start:stop]. The start is included, the stop is not:
word = "Python"
print(word[0:3]) # Pyt
print(word[3:]) # hon (to the end)Handy tools
len(word)gives the length (6here).word.upper()andword.lower()change case.text.strip()removes spaces from both ends.+joins (concatenates) strings:"Py" + "thon".
messy = " hello "
clean = messy.strip()
print(clean.upper()) # HELLO
print(len(clean)) # 5Your turn
Start with raw = " Floati ". Build a variable name that strips the spaces, then build shout as the upper-case version of name. Print shout (it should be FLOATI).
Lesson complete. Nice work.
Code · runs in your browser
Output
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.