DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 16 of 77
←PreviousPrevNextNext→

Modify Strings

String Methods

Strings in Python are immutable, meaning their contents cannot be changed in place. However, methods return new modified strings based on the original.

Example
text = "  hello World!  "

# Common modifications
upper = text.upper()      # '  HELLO WORLD!  '
lower = text.lower()      # '  hello world!  '
stripped = text.strip()   # 'hello World!'
replaced = text.replace("World", "Python")

print(upper)
print(lower)
print(stripped)
print(replaced)
Output
  HELLO WORLD!  
  hello world!  
hello World!
  hello Python!  

Case Conversion

Several built-in methods help with changing the case of strings. These are often used in formatting and text normalization.

MethodDescriptionExample
upper()Convert all characters to uppercase"hello".upper() → 'HELLO'
lower()Convert all characters to lowercase"Hello".lower() → 'hello'
title()Capitalize the first letter of each word"hello world".title() → 'Hello World'
capitalize()Capitalize only the first character"hello".capitalize() → 'Hello'
swapcase()Swap the case of all characters"HeLLo".swapcase() → 'hEllO'

Practical Examples

String modification is often applied when processing user input or preparing output. Common tasks include trimming whitespace, changing case for comparisons, and formatting messages with dynamic values.

Example
# Cleaning user input
user_input = "  UsERnaME  "
clean_input = user_input.strip().lower()
print(clean_input)  # 'username'

# Template replacement
template = "Hello {name}, your score is {score}"
personalized = template.format(name="Alice", score=95)
print(personalized)  # 'Hello Alice, your score is 95'
ℹ️ Note: Always normalize strings before comparing or storing them to ensure consistency.
Test your knowledge: Modify Strings
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 16 of 77
←PreviousPrevNextNext→