Python Strings
PythonStrings 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!
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!