Test and expand your knowledge of advanced Python string manipulation techniques with this quiz, covering methods, formatting, regular expressions, and encoding. Perfect for users looking to strengthen their expertise in handling and transforming Python strings.
String Immutability
What happens if you attempt to modify a single character of a Python string, such as s = 'hello'; s[0] = 'y'?
- You get a TypeError because strings are immutable.
- The string changes to 'yello'.
- A new string is created with the first character replaced.
- It silently fails without raising an error.
Advanced Slicing
Given the string text = 'abcdefghijkl', what is the result of text[4:10:2]?
- 'eghjkl'
- 'efghij'
- 'egik'
- 'ehj'
Using join() Method
What is the result of '.'.join(['2024', '06', '10'])?
- '2024-06-10'
- '2024,06,10'
- '2024.06.10'
- '20240610'
String Formatting: f-strings
How would you embed the variable name = 'Alice' and score = 98 into a string using f-strings?
- 'Name: ' + name + ', Score: ' + str(score)
- 'Name: %s, Score: %d' % (name, score)
- 'Name: {:name}, Score: {:score}'
- f'Name: {name}, Score: {score}'
find() vs index()
What distinguishes the find() method from the index() method in Python strings?
- find() returns -1 if the substring is not found, index() raises a ValueError.
- find() raises a ValueError, index() returns -1 if not found.
- Both methods behave identically.
- index() is case-insensitive, find() is not.
Regular Expressions: re.sub()
Which code replaces all digits in the string s = 'User123' with '#'?
- s.replace('d', '#')
- s.sub('#', 'd')
- re.sub(r'd', '#', s)
- re.replace(r'd', '#', s)
Splitting Strings: maxsplit
What is the result of 'apple,banana,cherry'.split(',', 1)?
- ['apple', 'banana,cherry']
- ['apple', 'banana', 'cherry']
- ['apple banana cherry']
- ['apple,banana', 'cherry']
String Encoding to Bytes
How do you convert the string phrase = 'Python!' into a UTF-8 encoded bytes object?
- phrase.encode('utf-8')
- phrase.to_bytes('utf-8')
- bytes(phrase, encoding='ascii')
- str.encode('Python!')
The zfill() Method
What is the output of '42'.zfill(5)?
- '00042'
- '00420'
- '04200'
- '42000'
Strip with Arguments
What is the result of '---Python!!!'.strip('-!')?
- '---Python'
- 'Python'
- 'Python!!!-'
- 'Python!!!'
Advanced Replacement
What is the output of 'a,b,c,d'.replace(',', ';', 2)?
- 'a;b,c,d'
- 'a;b;c,d'
- 'a,b;c;d'
- 'a;b;c;d'
isdecimal() vs isdigit()
Given s = '²3', what is the result of s.isdecimal() and s.isdigit()?
- isdecimal() returns False, isdigit() returns True
- isdecimal() returns True, isdigit() returns False
- Both return False
- Both return True
Casefold vs Lower
When should you prefer str.casefold() over str.lower() in Python string comparisons?
- When comparing string lengths
- When handling internationalized text for case-insensitive comparisons
- When removing all spaces from a string
- When converting only ASCII characters to lowercase
Partitioning Strings
What is the result of 'alpha-beta-gamma'.partition('-')?
- ('alpha-beta', '-', 'gamma')
- ['alpha', 'beta-gamma']
- ('alpha', 'beta', 'gamma')
- ('alpha', '-', 'beta-gamma')
String Multiplication
What does the expression '-*-' * 3 evaluate to in Python?
- '-*-*-*-'
- '-*-*-*-*-*-*-*-'
- '-*-*-*'
- '-*--*--*-'