Python String Manipulation: Advanced Concepts Quiz Quiz

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.

  1. String Immutability

    What happens if you attempt to modify a single character of a Python string, such as s = 'hello'; s[0] = 'y'?

    1. You get a TypeError because strings are immutable.
    2. The string changes to 'yello'.
    3. A new string is created with the first character replaced.
    4. It silently fails without raising an error.
  2. Advanced Slicing

    Given the string text = 'abcdefghijkl', what is the result of text[4:10:2]?

    1. 'eghjkl'
    2. 'efghij'
    3. 'egik'
    4. 'ehj'
  3. Using join() Method

    What is the result of '.'.join(['2024', '06', '10'])?

    1. '2024-06-10'
    2. '2024,06,10'
    3. '2024.06.10'
    4. '20240610'
  4. String Formatting: f-strings

    How would you embed the variable name = 'Alice' and score = 98 into a string using f-strings?

    1. 'Name: ' + name + ', Score: ' + str(score)
    2. 'Name: %s, Score: %d' % (name, score)
    3. 'Name: {:name}, Score: {:score}'
    4. f'Name: {name}, Score: {score}'
  5. find() vs index()

    What distinguishes the find() method from the index() method in Python strings?

    1. find() returns -1 if the substring is not found, index() raises a ValueError.
    2. find() raises a ValueError, index() returns -1 if not found.
    3. Both methods behave identically.
    4. index() is case-insensitive, find() is not.
  6. Regular Expressions: re.sub()

    Which code replaces all digits in the string s = 'User123' with '#'?

    1. s.replace('d', '#')
    2. s.sub('#', 'd')
    3. re.sub(r'd', '#', s)
    4. re.replace(r'd', '#', s)
  7. Splitting Strings: maxsplit

    What is the result of 'apple,banana,cherry'.split(',', 1)?

    1. ['apple', 'banana,cherry']
    2. ['apple', 'banana', 'cherry']
    3. ['apple banana cherry']
    4. ['apple,banana', 'cherry']
  8. String Encoding to Bytes

    How do you convert the string phrase = 'Python!' into a UTF-8 encoded bytes object?

    1. phrase.encode('utf-8')
    2. phrase.to_bytes('utf-8')
    3. bytes(phrase, encoding='ascii')
    4. str.encode('Python!')
  9. The zfill() Method

    What is the output of '42'.zfill(5)?

    1. '00042'
    2. '00420'
    3. '04200'
    4. '42000'
  10. Strip with Arguments

    What is the result of '---Python!!!'.strip('-!')?

    1. '---Python'
    2. 'Python'
    3. 'Python!!!-'
    4. 'Python!!!'
  11. Advanced Replacement

    What is the output of 'a,b,c,d'.replace(',', ';', 2)?

    1. 'a;b,c,d'
    2. 'a;b;c,d'
    3. 'a,b;c;d'
    4. 'a;b;c;d'
  12. isdecimal() vs isdigit()

    Given s = '²3', what is the result of s.isdecimal() and s.isdigit()?

    1. isdecimal() returns False, isdigit() returns True
    2. isdecimal() returns True, isdigit() returns False
    3. Both return False
    4. Both return True
  13. Casefold vs Lower

    When should you prefer str.casefold() over str.lower() in Python string comparisons?

    1. When comparing string lengths
    2. When handling internationalized text for case-insensitive comparisons
    3. When removing all spaces from a string
    4. When converting only ASCII characters to lowercase
  14. Partitioning Strings

    What is the result of 'alpha-beta-gamma'.partition('-')?

    1. ('alpha-beta', '-', 'gamma')
    2. ['alpha', 'beta-gamma']
    3. ('alpha', 'beta', 'gamma')
    4. ('alpha', '-', 'beta-gamma')
  15. String Multiplication

    What does the expression '-*-' * 3 evaluate to in Python?

    1. '-*-*-*-'
    2. '-*-*-*-*-*-*-*-'
    3. '-*-*-*'
    4. '-*--*--*-'