Master Python strings with this guide. Learn formatting with examples to improve your Python coding skills fast.
text = "hello"
print(text.upper()) # Output: "HELLO"
text = "HELLO"
print(text.lower()) # Output: "hello"
text = "python is fun"
print(text.capitalize()) # Output: "Python is fun"
text = "hello world"
print(text.title()) # Output: "Hello World"
text = "PyTHon"
print(text.swapcase()) # Output: "pYthON"
text = " hello world "
print(text.strip()) # Output: "hello world"
text = " hello"
print(text.lstrip()) # Output: "hello"
text = "hello "
print(text.rstrip()) # Output: "hello"
text = "Python"
print(text.center(10)) # Output: " Python "
print(text.ljust(10)) # Output: "Python "
print(text.rjust(10)) # Output: " Python"
Write a program that asks the user for their name and prints:
Example:
# Sample output for name "Alice"
Uppercase: ALICE
Lowercase: alice
Length: 5
Given a string with extra spaces:
sentence = " Python is fun! "
Write code to:
Print the word "Code"
:
find()
Method-1
if not found.text = "Hello, welcome to Python!"
position = text.find("welcome")
print(position) # Output: 7
replace()
Methodtext = "I love Python. Python is awesome!"
new_text = text.replace("Python", "coding")
print(new_text)
# Output: I love coding. coding is awesome!
comment = "This product is bad, really bad!"
cleaned = comment.replace("bad", "***")
print(cleaned)
# Output: This product is ***, really ***!
split()
MethodWhen you use split()
without any arguments, it splits the string using any whitespace as the separator. This includes:
" "
)"\t"
)"\n"
)Also, it automatically removes extra spaces, so multiple spaces are treated as one.
string.split()
sentence = "Python is fun"
words = sentence.split()
print(words)
# Output: ['Python', 'is', 'fun']
sentence = " Python is powerful "
words = sentence.split()
print(words)
# Output: ['Python', 'is', 'powerful']
text = "Learn\tPython\nNow"
parts = text.split()
print(parts)
# Output: ['Learn', 'Python', 'Now']
data = "apple,banana,orange"
fruits = data.split(",")
print(fruits)
# Output: ['apple', 'banana', 'orange']
csv_line = "John,Doe,25"
parts = csv_line.split(",")
print("First Name:", parts[0])
print("Last Name:", parts[1])
partition()
Method(before, separator, after)
email = "username@example.com"
result = email.partition("@")
print(result)
# Output: ('username', '@', 'example.com')
text = "hello world"
result = text.partition(":")
print(result)
# Output: ('hello world', '', '')
email = "jane.doe@gmail.com"
_, _, domain = email.partition("@")
print("Domain:", domain)
# Output: Domain: gmail.com
Method | Purpose | Returns |
---|---|---|
find() |
Finds substring position | Index or -1 |
replace() |
Replaces substrings | New string |
split() |
Splits into list | List of strings |
partition() |
Splits once, returns 3-part tuple | Tuple (before, sep, after) |
find()
and replace()
Ask the user to enter a sentence and a word. Use .find()
to print the position of that word.
# Example input: "Python is easy", word: "easy"
# Expected output: Word found at position 10
Write a program that replaces words like "bad"
or "ugly"
with "***"
in a sentence.
# Input: "This food is bad and ugly"
# Output: "This food is *** and ***"
Use .find()
inside a loop to count how many times a word appears.
split()
Write a program to split a user-provided sentence into words and print them one per line.
# Input: "Python is fun"
# Output:
# Python
# is
# fun
Split this string: "Alice,Math,85"
and print each part labeled (e.g., Name, Subject, Marks).
# Output:
# Name: Alice
# Subject: Math
# Marks: 85
Ask the user for a sentence and print the number of words in it using split()
.
partition()
Ask the user for an email and extract the username and domain using partition("@")
.
# Input: "user123@gmail.com"
# Output:
# Username: user123
# Domain: gmail.com
Partition a string at a given word and print whatβs before and after.
# Input: "The quick brown fox", partition word: "brown"
# Output:
# Before: The quick
# After: fox
Use partition()
on a string that doesnβt contain the separator and show the output tuple.