Master Python String Methods: 15 Essential Operations Explained
This tutorial walks through fifteen common Python string methods, providing clear syntax, practical code examples, and visual output screenshots to help readers understand how to find, replace, split, capitalize, and manipulate strings effectively.
Introduction
The article presents a collection of common Python string operations, illustrating each method with code snippets and the corresponding execution results.
1. Common Operations
find
Detects whether a substring exists in a string and returns its starting index, or -1 if not found. lstr.find(sub, start=0, end=len(lstr)) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.find("Museum"))
print(lstr.find("dada"))Result:
index
Works like find but raises an exception when the substring is absent. lstr.index(sub, start=0, end=len(lstr)) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.index("dada"))Result:
count
Returns the number of occurrences of a substring within the specified range. lstr.count(sub, start=0, end=len(lstr)) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.count("s"))Result:
replace
Replaces occurrences of a substring with another string; an optional count limits the number of replacements. lstr.replace(old, new, count) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.replace("s", "ttennd"))Result:
split
Splits a string by a delimiter; maxsplit limits the number of splits. lstr.split(sep=" ", maxsplit) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.split("to", 5))Result:
capitalize
Capitalizes the first character of the string. lstr.capitalize() Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.capitalize())Result:
title
Capitalizes the first character of each word in the string.
a = "hello itcast"
print(a.title()) # -> 'Hello Itcast'startswith
Checks if the string starts with the specified prefix, returning True or False. lstr.startswith(prefix) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.startswith('we'))Result:
endswith
Checks if the string ends with the specified suffix. lstr.endswith(suffix) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.endswith('hfs'))Result:
lower
Converts all uppercase characters in the string to lowercase. lstr.lower() Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.lower())Result:
upper
Converts all lowercase characters in the string to uppercase. lstr.upper() Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.upper())Result:
strip
Removes leading and trailing whitespace characters from the string.
a = "
\t itcast \t
"
print(a.strip()) # -> 'itcast'rfind
Similar to find but searches from the right end of the string. lstr.rfind(sub, start=0, end=len(lstr)) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.rfind('eijing'))Result:
rindex
Works like index but searches from the right. lstr.rindex(sub, start=0, end=len(lstr)) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.rindex('eijing'))Result:
partition
Splits the string at the first occurrence of the separator into a tuple (before, separator, after). lstr.partition(sep) Example:
lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.partition('eijing'))Result:
join
Concatenates an iterable of strings using the given separator. separator.join(iterable) Example:
li = ["my", "name", "is", "LY"]
print(" ".join(li))Result:
Conclusion
The article thoroughly explains fundamental Python string operations, covering slicing, indexing, and common pitfalls, and provides practical solutions to help readers improve their Python programming skills.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
