Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
String Basics & CreationString Indexing & SlicingCommon String MethodsString Formatting EssentialsFinding & Replacing TextString Testing & ValidationRegular Expression BasicsText Splitting & JoiningPractical String Projects
Python/String Manipulation/Common String Methods

🔧 Common String Methods — Transform Text with Built-in Functions

Python strings come with powerful built-in methods. These methods don't modify the original string (strings are immutable), but return new strings.


🎯 Case Conversion

Convert strings to uppercase or lowercase:

text = "Hello World"
print(text.upper())        # HELLO WORLD
print(text.lower())        # hello world
print(text.capitalize())   # Hello world
print(text.title())        # Hello World

💡 Stripping Whitespace

Remove leading and trailing whitespace:

messy = "  Python  \n"
print(messy.strip())       # Python (both sides)
print(messy.lstrip())      # Python  \n (left side)
print(messy.rstrip())      #   Python (right side)
print(messy.strip())       # Python

# Remove specific characters
url = "...example.com..."
print(url.strip("."))      # example.com

🎨 Replacing Text

Replace substrings with new text:

sentence = "I like apples. I eat apples daily."
print(sentence.replace("apples", "oranges"))
# I like oranges. I eat oranges daily.

# Replace only first N occurrences
print(sentence.replace("apples", "oranges", 1))
# I like oranges. I eat apples daily.

🔑 Splitting & Joining

Split strings into lists or join lists into strings:

# Splitting
sentence = "Python is awesome"
words = sentence.split()          # Default: split by whitespace
print(words)  # ['Python', 'is', 'awesome']

csv_data = "apple,banana,orange"
fruits = csv_data.split(",")
print(fruits)  # ['apple', 'banana', 'orange']

# Joining
word_list = ["Hello", "beautiful", "world"]
message = " ".join(word_list)
print(message)  # Hello beautiful world

# Join with different separator
numbers = ["1", "2", "3"]
print("-".join(numbers))  # 1-2-3

📊 Common String Methods Reference

MethodPurposeExample
`upper()`Convert to uppercase`"hello".upper()` → `'HELLO'`
`lower()`Convert to lowercase`"HELLO".lower()` → `'hello'`
`strip()`Remove whitespace`" hello ".strip()` → `'hello'`
`replace()`Replace text`"hi".replace("h", "H")` → `'Hi'`
`split()`Split into list`"a,b".split(",")` → `['a', 'b']`
`join()`Join from list`",".join(['a', 'b'])` → `'a,b'`
`find()`Find position`"hello".find("l")` → `2`
`startswith()`Check beginning`"hello".startswith("he")` → `True`
`endswith()`Check ending`"hello".endswith("lo")` → `True`

💡 Real-World Example: Processing User Input

# Clean and process user data
user_input = "  JOHN DOE  "
processed = user_input.strip().lower().replace(" ", "_")
print(processed)  # john_doe

# This works because methods return new strings
# that can be used immediately (method chaining)

🔑 Key Takeaways

ConceptRemember
Methods don't modifyString methods return new strings; originals stay the same
Method chainingCall multiple methods in sequence: `text.strip().upper()`
Split/join patternsUse `split()` to break apart, `join()` to reassemble
Case conversion`upper()`, `lower()`, `capitalize()`, `title()` cover most needs
Whitespace removal`strip()`, `lstrip()`, `rstrip()` for different sides

🔗 What's Next?

Ready to format strings dynamically? Learn about f-strings and formatting.


Ready to practice? Challenges | Quiz


Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn