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.

๐Ÿงช 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






๐Ÿ”ด Advanced Exercises







๐Ÿง  Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




๐Ÿง  Python Advanced

Object-Oriented Programming in Python (OOP)

More...

๐Ÿง  Modules