Common Built‑in String Methods in Python
This article introduces Python’s built‑in string class and explains ten commonly used string methods—including center, count, find, swapcase, startswith/endswith, split, case conversions, justification, strip, and zfill—detailing their syntax, parameters, and example usages while emphasizing that they return new strings without altering the original.
Strings are sequences of characters. Python’s built‑in str class represents Unicode strings. In addition to common operations, strings have several additional methods. The figure below shows all available methods:
In this article we will learn some of the most commonly used methods. Important point: all string methods always return a new value and do not modify the original string.
1. center()
The center() method aligns a string within a given width using a specified fill character (default is a space).
Syntax:
str.center(length, fillchar) where length is the total width (required) and fillchar is the character used for padding (optional).
Example:
2. count()
The count() method returns the number of occurrences of a specified substring.
Syntax:
str.count(value, start, end) where value is the substring to search (required), start is the starting index (optional), and end is the ending index (optional).
Example:
3. find()
The find() method returns the lowest index of a substring; if the substring is not found, it returns -1 . The related rfind() returns the highest index.
Syntax:
str.find(value, start, end) where value is the substring to search (required), start and end are optional bounds.
Example:
4. swapcase()
The swapcase() method returns a copy of the string with uppercase letters converted to lowercase and vice‑versa.
Syntax:
string.swapcase()
Example:
5. startswith() and endswith()
startswith() returns True if the string begins with the specified value; otherwise False . endswith() does the same for the string’s suffix.
Syntax:
string.startswith(value, start, end)
string.endswith(value, start, end)
Parameters: value (required), start and end (optional).
Example:
6. split()
The split() method returns a list of words in the string, using whitespace as the default separator.
Syntax:
string.split(sep, maxsplit)
sep : delimiter (optional, default is whitespace).
maxsplit : maximum number of splits (optional, default -1 meaning all).
Version note: rsplit() splits from the right.
Example:
7. String case conversion
7.1 capitalize()
Converts only the first character of the string to uppercase.
Syntax: string.capitalize()
7.2 upper()
Converts all alphabetic characters in the string to uppercase.
Syntax: string.upper()
7.3 title()
Converts the first character of each word to uppercase.
Syntax: string.title()
8. ljust() and rjust()
ljust() left‑justifies the string using a specified fill character (default space). rjust() right‑justifies it.
Syntax:
string.ljust(length, character) or string.rjust(length, character)
length (required): total width of the result.
character (optional): fill character, default is space.
Example:
9. strip()
The strip() method returns a copy of the string with leading and trailing characters removed (default is space).
Syntax:
string.strip(character) where character is an optional set of characters to remove.
Version notes:
rstrip() : removes characters from the right end.
lstrip() : removes characters from the left end.
Example:
10. zfill()
The zfill() method pads the string on the left with zeros to reach a specified width.
Syntax:
string.zfill(width)
width : desired total length; if smaller than the original length, no padding is added.
Example:
Conclusion
These are some useful built‑in string methods in Python. There are additional methods not covered here that are also important. For deeper details, refer to the official Python documentation.
Disclaimer:
This article was compiled from the web; copyright belongs to the original author. If any source information is incorrect or infringes rights, please contact us for removal or authorization.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.