Master Python strings with this guide. Learn string manipulations, methods, slicing, and formatting with examples to improve your Python coding skills fast.
"hello"
, 'Python'
, "1234"
name = "Alice"
greeting = 'Hello'
first = "Hello"
second = "World"
result = first + " " + second # Output: "Hello World"
word = "Hi"
print(word * 3) # Output: "HiHiHi"
text = "Python"
print(text[0]) # Output: "P"
print(text[-1]) # Output: "n"
text = "Python"
print(text[0:3]) # Output: "Pyt"
print(text[2:]) # Output: "thon"
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"
: