New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials Python Python Strings

Python Strings

Python

Strings are sequences of characters. Python has powerful built-in string methods.

Key Points

  • Strings are immutable — cannot be changed in place
  • Use single '' or double "" quotes
  • f-strings (f"...") are the modern way to format
  • Slicing: str[start:end:step]
  • Common methods: upper(), lower(), strip(), split(), replace()
Example — PYTHON
# String basics
msg = "Hello, Python!"
print(msg.upper())
print(msg.lower())
print(len(msg))
# Slicing
print(msg[0:5])     # Hello
print(msg[::-1])     # Reverse string
# String methods
email = "  [email]  "
print(email.strip())
print(email.strip().split("@"))
name = "rahul sharma"
print(name.title())   # Rahul Sharma
# Check content
phone = "9876543210"
print(phone.isdigit())   # True
pan = "ABCDE1234F"
print(pan.isalnum())     # True
# Replace
sentence = "I love Java"
print(sentence.replace("Java", "Python"))
# Multi-line string
address = """Rahul Sharma
123 MG Road
Pune, Maharashtra"""
print(address)
# String repetition
print("=" * 30)
print("Python is fun! " * 3)
Result
HELLO, PYTHON!
hello, python!
14
Hello
!nohtyP ,olleH
[email]
['rahul', 'gmail.com']
Rahul Sharma
True
True
I love Python
Rahul Sharma
123 MG Road
Pune, Maharashtra
==============================
Python is fun! Python is fun! Python is fun!