Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Built-in String Methods in Python
Python provides a rich set of built-in string methods for manipulating and working with text data. These methods cover everything from case conversion and formatting to searching and validation.
Case Conversion Methods
Methods for changing the case of characters in strings ?
text = "hello WORLD"
print("Original:", text)
print("capitalize():", text.capitalize())
print("upper():", text.upper())
print("lower():", text.lower())
print("title():", text.title())
print("swapcase():", text.swapcase())
Original: hello WORLD capitalize(): Hello world upper(): HELLO WORLD lower(): hello world title(): Hello World swapcase(): HELLO world
String Validation Methods
Methods that return True or False based on string content ?
text1 = "Hello123"
text2 = "hello"
text3 = "123"
text4 = " "
print("isalnum():", text1.isalnum()) # Letters and numbers
print("isalpha():", text2.isalpha()) # Only letters
print("isdigit():", text3.isdigit()) # Only digits
print("isspace():", text4.isspace()) # Only whitespace
print("islower():", text2.islower()) # Lowercase letters
print("isupper():", "HELLO".isupper()) # Uppercase letters
isalnum(): True isalpha(): True isdigit(): True isspace(): True islower(): True isupper(): True
Search and Replace Methods
Methods for finding and replacing text within strings ?
text = "Python is great. Python is powerful."
print("find('Python'):", text.find("Python"))
print("count('Python'):", text.count("Python"))
print("replace('Python', 'Java'):", text.replace("Python", "Java"))
print("startswith('Python'):", text.startswith("Python"))
print("endswith('powerful.'):", text.endswith("powerful."))
find('Python'): 0
count('Python'): 2
replace('Python', 'Java'): Java is great. Java is powerful.
startswith('Python'): True
endswith('powerful.'): True
String Formatting and Padding
Methods for formatting and aligning strings ?
text = "Python"
print("center(20, '*'):", text.center(20, '*'))
print("ljust(15, '-'):", text.ljust(15, '-'))
print("rjust(15, '-'):", text.rjust(15, '-'))
print("zfill(10):", "42".zfill(10))
center(20, '*'): *******Python******* ljust(15, '-'): Python--------- rjust(15, '-'): ---------Python zfill(10): 0000000042
String Splitting and Joining
Methods for breaking strings apart and combining them ?
text = "apple,banana,cherry"
words = ["Python", "is", "awesome"]
print("split(','):", text.split(','))
print("join with ' ':", ' '.join(words))
print("splitlines():", "Line1\nLine2\nLine3".splitlines())
split(','): ['apple', 'banana', 'cherry']
join with ' ': Python is awesome
splitlines(): ['Line1', 'Line2', 'Line3']
String Cleaning Methods
Methods for removing unwanted characters ?
text = " Hello World "
print("Original:", repr(text))
print("strip():", repr(text.strip()))
print("lstrip():", repr(text.lstrip()))
print("rstrip():", repr(text.rstrip()))
Original: ' Hello World ' strip(): 'Hello World' lstrip(): 'Hello World ' rstrip(): ' Hello World'
Complete Methods Reference
| Method | Description |
|---|---|
| capitalize() | Capitalizes first letter of string |
| center(width, fillchar) | Returns a centered string padded to width |
| count(str, beg, end) | Counts occurrences of substring |
| endswith(suffix, beg, end) | Checks if string ends with suffix |
| find(str, beg, end) | Finds index of substring (-1 if not found) |
| index(str, beg, end) | Like find() but raises exception if not found |
| isalnum() | True if all characters are alphanumeric |
| isalpha() | True if all characters are alphabetic |
| isdigit() | True if all characters are digits |
| islower() | True if all cased characters are lowercase |
| isspace() | True if string contains only whitespace |
| istitle() | True if string is properly titlecased |
| isupper() | True if all cased characters are uppercase |
| join(seq) | Joins sequence elements with string as separator |
| ljust(width, fillchar) | Left-justifies string in field of width |
| lower() | Converts string to lowercase |
| lstrip(chars) | Removes leading whitespace or characters |
| replace(old, new, max) | Replaces occurrences of old with new |
| rfind(str, beg, end) | Finds last occurrence of substring |
| rjust(width, fillchar) | Right-justifies string in field of width |
| rstrip(chars) | Removes trailing whitespace or characters |
| split(delimiter, maxsplit) | Splits string into list using delimiter |
| splitlines(keepends) | Splits string at line breaks |
| startswith(prefix, beg, end) | Checks if string starts with prefix |
| strip(chars) | Removes leading and trailing whitespace |
| swapcase() | Swaps case of all letters |
| title() | Returns titlecased version of string |
| upper() | Converts string to uppercase |
| zfill(width) | Pads string with zeros on the left |
Conclusion
Python's built-in string methods provide comprehensive functionality for text manipulation. These methods are essential for string processing tasks like validation, formatting, searching, and transformation in Python applications.
