Fundamentals 9 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Common Built‑in String Methods in Python

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:

Python built‑in string functions
Python built‑in string functions

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:

center example
center 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:

count example
count 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:

find example
find 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:

swapcase example
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:

startswith/endswith example
startswith/endswith 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:

split example
split 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()
title example
title example

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:

ljust/rjust example
ljust/rjust 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:

strip example
strip 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:

zfill example
zfill 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonUnicodeprogramming fundamentalstext processingString Methods
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.