0
0
Pythonprogramming~5 mins

Common string transformations in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the str.upper() method do in Python?
It converts all lowercase letters in a string to uppercase letters. For example, 'hello'.upper() returns 'HELLO'.
Click to reveal answer
beginner
How can you remove whitespace from the start and end of a string in Python?
Use the str.strip() method. It removes spaces, tabs, and newlines from both ends of the string. For example, ' hello '.strip() returns 'hello'.
Click to reveal answer
intermediate
What is the difference between str.replace(old, new) and str.translate()?
str.replace(old, new) replaces all occurrences of a substring with another substring. str.translate() replaces characters based on a translation table, useful for multiple character replacements at once.
Click to reveal answer
beginner
How do you split a string into a list of words in Python?
Use the str.split() method without arguments. It splits the string at whitespace and returns a list of words. For example, 'a b c'.split() returns ['a', 'b', 'c'].
Click to reveal answer
beginner
What does str.join(iterable) do?
It joins elements of an iterable (like a list) into a single string, inserting the original string between elements. For example, '-'.join(['a', 'b', 'c']) returns 'a-b-c'.
Click to reveal answer
Which method converts all characters in a string to lowercase?
Astr.strip()
Bstr.lower()
Cstr.capitalize()
Dstr.upper()
What does ' hello '.strip() return?
A'hello'
B' hello '
C'hello '
D' hello'
How do you split the string 'one,two,three' into a list by commas?
A'one,two,three'.join(',')
B'one,two,three'.split()
C'one,two,three'.split(',')
D'one,two,three'.replace(',', ' ')
What does '-'.join(['a', 'b', 'c']) produce?
A'a-b-c'
B'abc'
C'a b c'
D'-a-b-c-'
Which method replaces all occurrences of 'cat' with 'dog' in a string?
Astr.translate('cat', 'dog')
Bstr.split('cat')
Cstr.strip('cat')
Dstr.replace('cat', 'dog')
Explain how to convert a string to uppercase and then remove spaces from both ends.
Think about calling one method after another on the string.
You got /3 concepts.
    Describe how to split a sentence into words and then join them back with commas.
    First break the string, then combine with a new separator.
    You got /4 concepts.