Strings are one of the most main data types in Python, and master them is essential for any beginner or knowledgeable developer. In this article, we’ll explore Python string questions that will inspect your knowledge and help you understand how strings work in real coding scenarios. These questions range from basic string operations to slightly advanced topics like slicing, formatting, and built-in string methods. Whether you’re preparing for an interview, practicing for an exam, or simply looking to sharpen your Python skills, these exercises will give you the hands-on experience you need. Each question is followed by a clear, concise answer so you can instantly check your understanding. By the end, you’ll have a stronger grasp of Python strings, making it easier to manipulate text, solve problems, and write clean, efficient code in your projects.
Things We’ll Cover
In this article, you’ll learn and practice:
- Basic string creation and printing
- String slicing and indexing
- Common string methods (upper(), lower(), strip(), etc.)
- String concatenation and repetition
- String formatting techniques
- Checking substrings in a string
1. Create a string and print it.
text = "Hello, Python!" print(text) # Output: Hello, Python!
2. Find the length of a string.
text = "Python" print(len(text)) # Output: 6
3. Access the first character of a string.
text = "Python" print(text[0]) # Output: P
4. Access the last character of a string.
text = "Python" print(text[-1]) # Output: n
5. Slice the first 3 characters of a string.
text = "Python" print(text[:3]) # Output: Pyt
6. Slice the last 3 characters of a string.
text = "Python" print(text[-3:]) # Output: hon
7. Convert a string to uppercase.
text = "python" print(text.upper()) # Output: PYTHON
8. Convert a string to lowercase.
text = "PYTHON" print(text.lower()) # Output: python
9. Remove extra spaces from a string.
text = " Python " print(text.strip()) # Output: Python
10. Replace a word in a string.
text = "I love Java" print(text.replace("Java", "Python")) # Output: I love Python
11. Check if a string starts with a specific letter.
text = "Python" print(text.startswith("Py")) # Output: True
12. Check if a string ends with a specific letter.
text = "Python" print(text.endswith("on")) # Output: True
13. Count the occurrences of a character.
text = "banana" print(text.count("a")) # Output: 3
14. Find the position of a substring.
text = "I love Python" print(text.find("Python")) # Output: 7
15. Join a list of words into a string.
words = ["I", "love", "Python"] print(" ".join(words)) # Output: I love Python
16. Split a string into a list.
text = "I love Python" print(text.split()) # Output: ['I', 'love', 'Python']
17. Check if a string contains only alphabets.
text = "Python" print(text.isalpha()) # Output: True
18. Check if a string contains only digits.
text = "12345" print(text.isdigit()) # Output: True
19. Repeat a string multiple times.
text = "Hi! " print(text * 3) # Output: Hi! Hi! Hi!
20. Format a string using f-strings.
name = "Alice" print(f"Hello, {name}!") # Output: Hello, Alice!
21. Format a string using format().
age = 25 print("I am {} years old.".format(age)) # Output: I am 25 years old.
22. Reverse a string.
text = "Python" print(text[::-1]) # Output: nohtyP
23. Check if a substring exists in a string.
text = "I love Python" print("Python" in text) # Output: True
24. Remove all occurrences of a character.
text = "banana" print(text.replace("a", "")) # Output: bnn
25. Swap case of a string.
text = "PyThOn" print(text.swapcase()) # Output: pYtHoN