Share Your Feedback

Python String Tutorial: Learn String Methods & Formatting

Master Python strings with this guide. Learn string manipulations, methods, slicing, and formatting with examples to improve your Python coding skills fast.

String Manipulations

What is a String?

  • A string is a sequence of characters enclosed in quotes.
    • Examples: "hello", 'Python', "1234"

Creating Strings:

name = "Alice"
greeting = 'Hello'

Common String Operations:

  1. Concatenation (joining strings):
    first = "Hello"
    second = "World"
    result = first + " " + second  # Output: "Hello World"
    
  2. Repetition:
    word = "Hi"
    print(word * 3)  # Output: "HiHiHi"
    
  3. Indexing:
    text = "Python"
    print(text[0])  # Output: "P"
    print(text[-1]) # Output: "n"
    
  4. Slicing:
    text = "Python"
    print(text[0:3])  # Output: "Pyt"
    print(text[2:])   # Output: "thon"
    

Formatting Strings

Formatting Strings – Adjusting Case

1. Convert to Uppercase:

text = "hello"
print(text.upper())  # Output: "HELLO"

2. Convert to Lowercase:

text = "HELLO"
print(text.lower())  # Output: "hello"

3. Capitalize First Letter:

text = "python is fun"
print(text.capitalize())  # Output: "Python is fun"

4. Title Case (First letter of each word capitalized):

text = "hello world"
print(text.title())  # Output: "Hello World"

5. Swap Case:

text = "PyTHon"
print(text.swapcase())  # Output: "pYthON"

Formatting Strings – Adding and Removing Spaces**

1. Removing Extra Spaces:

  • Remove leading and trailing spaces:
    text = "   hello world   "
    print(text.strip())  # Output: "hello world"
    
  • Remove only leading spaces:
    text = "   hello"
    print(text.lstrip())  # Output: "hello"
    
  • Remove only trailing spaces:
    text = "hello   "
    print(text.rstrip())  # Output: "hello"
    

2. Adding Spaces or Padding:

  • Center-align with padding:
    text = "Python"
    print(text.center(10))  # Output: "  Python  "
    
  • Left-align:
    print(text.ljust(10))  # Output: "Python    "
    
  • Right-align:
    print(text.rjust(10))  # Output: "    Python"
    

Practice Tasks

Task 1: String Info

Write a program that asks the user for their name and prints:

  • The name in all uppercase
  • The name in all lowercase
  • The number of characters in the name

Example:

# Sample output for name "Alice"
Uppercase: ALICE  
Lowercase: alice  
Length: 5

Task 2: Remove Extra Spaces

Given a string with extra spaces:

sentence = "   Python is fun!   "

Write code to:

  • Remove the spaces
  • Print the cleaned-up sentence

Task 3: Align a String

Print the word "Code":

  • Centered in a 20-character space
  • Left-aligned in a 20-character space
  • Right-aligned in a 20-character space


🧠 Practice & Progress

Explore More Topics

🐣 Python for Beginners

🧠 Python Advanced