Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Python Sets
- File management operations
Python Strings
String capitalization
Python offers several methods to manipulate the capitalization of characters within a string. You can convert all characters to uppercase using upper(), convert all to lowercase with lower(), capitalize the first letter of each word with title(), or capitalize only the first letter of the entire string with capitalize(). These functions are essential for various text processing tasks, such as standardizing user input or formatting output.
Let’s dive into each of these methods with explanations and examples.
1. upper(): Convert to uppercase
The upper() method returns a new string where all characters are converted to uppercase.
Syntax:
Let’s dive into each of these methods with explanations and examples.
1. upper(): Convert to uppercase
The upper() method returns a new string where all characters are converted to uppercase.
Syntax:
string.upper()
Example:
text = "hello world" uppercase_text = text.upper() print(uppercase_text) # Output: "HELLO WORLD"
In this example, all lowercase letters are converted to uppercase.
2. lower(): Convert to lowercase
The lower() method returns a new string where all characters are converted to lowercase.
Syntax:
string.lower()
Example:
text = "HELLO WORLD" lowercase_text = text.lower() print(lowercase_text) # Output: "hello world"
Here, all uppercase letters are converted to lowercase.
3. capitalize(): Capitalize the first letter
The capitalize() method returns a new string where the first character is capitalized and all other characters are converted to lowercase.
Syntax:
string.capitalize()
Example:
text = "python programming" capitalized_text = text.capitalize() print(capitalized_text) # Output: "Python programming"
In this example, only the first letter of the string is capitalized, while the rest is in lowercase.
4. title(): Capitalize each word
The title() method returns a new string where the first character of each word is capitalized, and all other characters in each word are lowercase. This is useful for formatting titles or headings.
Syntax:
string.title()
Example
text = "python programming is fun" title_text = text.title() print(title_text) # Output: "Python Programming Is Fun"
In this case, the first letter of each word is capitalized, while the remaining letters in each word are lowercase.
5. swapcase(): Swap Case of Each Character
The swapcase() method returns a new string where the case of each character is swapped. Uppercase letters are converted to lowercase, and lowercase letters are converted to uppercase.
Syntax:
string.swapcase()
Example
text = "Hello World" swapped_text = text.swapcase() print(swapped_text) # Output: "hELLO wORLD"
In this example, all uppercase letters are converted to lowercase and vice versa.
Additional notes:
- Remember that strings in Python are immutable, meaning the original string is not modified. All these methods return a new string with the case modifications.
- These methods only affect alphabetic characters (`a-z` and `A-Z`). Non-alphabetic characters, such as numbers or punctuation, remain unchanged.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
Which Python method converts all characters in a string to uppercase?
Ccapitalize()
Aupper()
Blower()
Check Answer
Which Python method converts all characters in a string to lowercase?
Aupper()
Blower()
Ctitle()
Check Answer
Which Python method capitalizes only the first letter of the entire string?
Aupper()
Blower()
Ccapitalize()
Check Answer
Which Python method capitalizes the first letter of each word in a string?
Bcapitalize()
Clower()
Atitle()
Check Answer
Which Python method swaps the case of each character in a string (uppercase to lowercase and vice versa)?
Atitle()
Bcapitalize()
Cswapcase()
Check Answer