WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Intermediate · question 21 of 100

What are Python’s built-in methods for string manipulation? Can you provide some examples?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python provides a wide range of built-in methods for string manipulation. Here are some of the most commonly used methods:

len(): returns the length of a string

    string = "Hello World"
    print(len(string)) # Output: 11

lower(): returns a lowercase version of a string

    string = "Hello World"
    print(string.lower()) # Output: hello world

upper(): returns an uppercase version of a string

    string = "Hello World"
    print(string.upper()) # Output: HELLO WORLD

strip(): removes leading and trailing whitespace from a string

    string = "   Hello World   "
    print(string.strip()) # Output: "Hello World"

split(): splits a string into a list of substrings, based on a specified delimiter

    string = "Hello,World"
    print(string.split(",")) # Output: ['Hello', 'World']

replace(): replaces all occurrences of a substring with another substring

    string = "Hello World"
    print(string.replace("World", "Python")) # Output: "Hello Python"

find(): searches for a substring within a string, and returns the index of the first occurrence

    string = "Hello World"
    print(string.find("o")) # Output: 4

startswith(): returns True if a string starts with a specified substring, otherwise False

    string = "Hello World"
    print(string.startswith("Hello")) # Output: True

endswith(): returns True if a string ends with a specified substring, otherwise False

    string = "Hello World"
    print(string.endswith("World")) # Output: True

join(): joins a list of strings into a single string, using a specified separator

    my_list = ['Hello', 'World']
    separator = ', '
    print(separator.join(my_list)) # Output: "Hello, World"

In summary, Python provides a wide range of built-in methods for string manipulation, such as len(), lower(), upper(), strip(), split(), replace(), find(), startswith(), endswith(), and join(). These methods can be used to manipulate strings in various ways, such as converting between uppercase and lowercase, splitting and joining substrings, and searching for and replacing substrings.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics