Lessons
Python Basics
Python Variables
Operators in Python
Conditional Statements in Python
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Loops in Python
Python Arrays and Functions
Conclusion
Python String Format
Python String Methods
Python provides a variety of built-in string methods that allow you to manipulate and format strings in many ways. Each method returns a new string; the original string remains unchanged.
Here’s an overview of some common string methods:
1. capitalize()
Converts the first character of the string to uppercase.
python
1text = "hello world"
2print(text.capitalize()) # Output: "Hello world"
2. casefold()
Converts the string to lowercase, more aggressive than lower()
, useful for case-insensitive comparisons.
python
1text = "HELLO"
2print(text.casefold()) # Output: "hello"
3. center()
Centers the string, padding it with spaces or a specified character.
python
1text = "Hello"
2print(text.center(10, '*')) # Output: "**Hello***"
4. count()
Counts how many times a substring appears in a string.
python
1text = "banana"
2print(text.count('a')) # Output: 3
5. encode()
Encodes the string using the specified encoding (default is UTF-8).
python
1text = "Hello"
2print(text.encode()) # Output: b'Hello' (in bytes)
6. endswith()
Checks if the string ends with a specified suffix.
python
1text = "hello.txt"
2print(text.endswith('.txt')) # Output: True
7. find()
Finds the first occurrence of a substring and returns its position. Returns -1
if not found.
python
1text = "Hello World"
2print(text.find("World")) # Output: 6
8. format()
Inserts variables into a string using placeholders.
python
1name = "John"
2age = 30
3print("My name is {} and I am {} years old.".format(name, age)) # Output: My name is John and I am 30 years old.
9. isalnum()
Returns True
if the string consists only of alphanumeric characters (letters and numbers).
python
1text = "Hello123"
2print(text.isalnum()) # Output: True
10. isalpha()
Returns True
if the string consists only of alphabetic characters.
python
1text = "Hello"
2print(text.isalpha()) # Output: True
11. islower()
Checks if all characters in the string are lowercase.
python
1text = "hello"
2print(text.islower()) # Output: True
12. join()
Joins elements of an iterable (such as a list) into a single string, with a specified separator.
python
1list_of_words = ['Hello', 'World']
2print(" ".join(list_of_words)) # Output: "Hello World"
13. replace()
Replaces occurrences of a substring with another string.
python
1text = "Hello World"
2print(text.replace("World", "Universe")) # Output: "Hello Universe"
14. split()
Splits the string at the specified separator and returns a list.
python
1text = "apple,banana,grape"
2print(text.split(',')) # Output: ['apple', 'banana', 'grape']
15. strip()
Removes leading and trailing whitespace or specified characters.
python
1text = " Hello World "
2print(text.strip()) # Output: "Hello World"
16. upper()
Converts the string to uppercase.
python
1text = "hello"
2print(text.upper()) # Output: "HELLO"
17. title()
Converts the first character of each word to uppercase.
python
1text = "hello world"
2print(text.title()) # Output: "Hello World"
18. zfill()
Fills the string with leading zeros until it reaches a specified length.
python
1text = "42"
2print(text.zfill(5)) # Output: "00042"