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
1 2
text = "hello world" print(text.capitalize()) # Output: "Hello world"
2. casefold()
Converts the string to lowercase, more aggressive than lower()
, useful for case-insensitive comparisons.
python
1 2
text = "HELLO" print(text.casefold()) # Output: "hello"
3. center()
Centers the string, padding it with spaces or a specified character.
python
1 2
text = "Hello" print(text.center(10, '*')) # Output: "**Hello***"
4. count()
Counts how many times a substring appears in a string.
python
1 2
text = "banana" print(text.count('a')) # Output: 3
5. encode()
Encodes the string using the specified encoding (default is UTF-8).
python
1 2
text = "Hello" print(text.encode()) # Output: b'Hello' (in bytes)
6. endswith()
Checks if the string ends with a specified suffix.
python
1 2
text = "hello.txt" print(text.endswith('.txt')) # Output: True
7. find()
Finds the first occurrence of a substring and returns its position. Returns -1
if not found.
python
1 2
text = "Hello World" print(text.find("World")) # Output: 6
8. format()
Inserts variables into a string using placeholders.
python
1 2 3
name = "John" age = 30 print("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
1 2
text = "Hello123" print(text.isalnum()) # Output: True
10. isalpha()
Returns True
if the string consists only of alphabetic characters.
python
1 2
text = "Hello" print(text.isalpha()) # Output: True
11. islower()
Checks if all characters in the string are lowercase.
python
1 2
text = "hello" print(text.islower()) # Output: True
12. join()
Joins elements of an iterable (such as a list) into a single string, with a specified separator.
python
1 2
list_of_words = ['Hello', 'World'] print(" ".join(list_of_words)) # Output: "Hello World"
13. replace()
Replaces occurrences of a substring with another string.
python
1 2
text = "Hello World" print(text.replace("World", "Universe")) # Output: "Hello Universe"
14. split()
Splits the string at the specified separator and returns a list.
python
1 2
text = "apple,banana,grape" print(text.split(',')) # Output: ['apple', 'banana', 'grape']
15. strip()
Removes leading and trailing whitespace or specified characters.
python
1 2
text = " Hello World " print(text.strip()) # Output: "Hello World"
16. upper()
Converts the string to uppercase.
python
1 2
text = "hello" print(text.upper()) # Output: "HELLO"
17. title()
Converts the first character of each word to uppercase.
python
1 2
text = "hello world" print(text.title()) # Output: "Hello World"
18. zfill()
Fills the string with leading zeros until it reaches a specified length.
python
1 2
text = "42" print(text.zfill(5)) # Output: "00042"