Learn with Yasir

Share Your Feedback

Learn Python String Methods & Formatting with Coding Exercises.

Test your Python skills with Coding Exercises on string methods and formatting. Great for beginners learning Python strings through hands-on practice.

Topic: strings


๐Ÿงช Coding Exercises

๐ŸŸข Beginner Exercises

  1. โœ… String Information
  2. Ask the user for their name and print:
    - The name in all uppercase
    - The name in all lowercase
    - The number of characters in the name
    

    Requirements

    • - Use `input()`, `upper()`, `lower()`, and `len()`

    Input

    name = input("Enter your name: ")
    

    Expected Output

    Uppercase: JOHN
    Lowercase: john
    Length: 4
    

  3. โœ… Remove Extra Spaces
  4. Given a string with leading and trailing spaces, remove them and print the cleaned sentence.
    

    Requirements

    • - Use the `strip()` method.

    Input

    sentence = "   Python is fun!   "
    

    Expected Output

    Cleaned Sentence: Python is fun!
    

  5. โœ… String Alignment
  6. Align the word "Code" within a 20-character width using different alignments.
    

    Requirements

    • - Use `center()`, `ljust()`, and `rjust()`

    Input

    word = "Code"
    

    Expected Output

    Centered :        Code        
    Left     : Code               
    Right    :               Code
    

  7. โœ… String Slicing
  8. Perform slicing on the string "Python Programming":
    - Extract and print the first 6 characters
    - Extract and print the last 11 characters
    - Extract and print characters from index 7 to 17
    

    Requirements

    • - Use slicing syntax with `[]`

    Input

    text = "Python Programming"
    

    Expected Output

    First 6 characters: Python
    Last 11 characters: Programming
    Characters 7 to 17: Programming
    

  9. โœ… String Formatting
  10. Given name and age variables, print: "My name is Alice and I am 30 years old."
    

    Requirements

    • - Use f-strings or `format()` for string formatting.

    Input

    name = "Alice"
    age = 30
    

    Expected Output

    My name is Alice and I am 30 years old.
    

๐ŸŸก Intermediate Exercises

  1. โœ… Replace Second Character Occurrence
  2. Write a Python function `replace_second_occurrence(main_string, old_char, new_char)` 
    - that replaces the second occurrence of `old_char` with `new_char` in `main_string`. 
    - If `old_char` appears fewer than two times, the string should remain unchanged.
    

    Requirements

    • - Define a function named `replace_second_occurrence` that accepts three arguments: - the main string, the character to be replaced (old_char), and the new character (new_char). - Use `str.find()` or `str.index()` to locate the position of the first occurrence of `old_char`. - Use string slicing and concatenation to reconstruct the string, ensuring only the second occurrence is replaced. - Handle cases where the `old_char` does not appear at least twice in the string.

    Input

    # Example 1: Basic replacement
    main_string_1 = "hello world"
    old_char_1 = "l"
    new_char_1 = "X"
    
    # Example 2: Another replacement
    main_string_2 = "banana"
    old_char_2 = "a"
    new_char_2 = "Z"
    
    # Example 3: Character appears only once
    main_string_3 = "apple"
    old_char_3 = "P" # Case sensitive, no match for 'P'
    new_char_3 = "Q"
    
    # Example 4: Character does not exist
    main_string_4 = "test"
    old_char_4 = "z"
    new_char_4 = "Q"
    

    Expected Output

    Original: hello world, Old Char: l, New Char: X -> Result: helXo world
    Original: banana, Old Char: a, New Char: Z -> Result: banZna
    Original: apple, Old Char: P, New Char: Q -> Result: apple
    Original: test, Old Char: z, New Char: Q -> Result: test
    

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


๐Ÿ”ด Advanced Exercises

๐Ÿง  Practice & Progress

Explore More Topics

๐Ÿ“˜ Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




๐Ÿง  Python Advanced

Object-Oriented Programming in Python (OOP)

More...

๐Ÿง  Modules