Fundamentals 9 min read

Master 15 Essential Python String Methods with Real Code Examples

This article walks through fifteen core Python string methods—such as find, index, count, replace, split, capitalize, title, startswith, endswith, lower, upper, strip, rfind, rindex, partition, and join—explaining their syntax, showing runnable code snippets, and displaying the actual output images for each operation.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master 15 Essential Python String Methods with Real Code Examples

1. Common String Operations

Using the sample string lstr = 'welcome to Beijing Museumitcpps fdsfs', the article demonstrates how to manipulate and query Python strings.

<1> find

Detects whether a substring exists in lstr; returns the starting index if found, otherwise -1. lstr.find(str, start=0, end=len(lstr)) Example:

lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.find("Museum"))
print(lstr.find("dada"))

Result:

<2> index

Works like find() but raises a ValueError if the substring is not found. lstr.index(str, start=0, end=len(lstr)) Example:

lstr = 'welcome to Beijing Museumitcpps fdsfs'
print(lstr.index("dada"))

Result:

<3> count

Returns the number of occurrences of a substring within the specified range. lstr.count(str, start=0, end=len(lstr)) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.count("s"))

Result:

<4> replace

Replaces occurrences of str1 with str2; an optional count limits the number of replacements. lstr.replace(str1, str2, count) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.replace("s", "ttennd"))

Result:

<5> split

Splits lstr by a delimiter; maxsplit limits the number of splits. lstr.split(sep=" ", maxsplit=None) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.split("to", 5))

Result:

<6> capitalize

Capitalizes the first character of the string. lstr.capitalize() Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.capitalize())

Result:

<7> title

Capitalizes the first character of each word.

a = "hello itcast"
print(a.title())  # 'Hello Itcast'

<8> startswith

Checks if the string begins with the given prefix; returns True or False. lstr.startswith(prefix) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.startswith('we'))

Result:

<9> endswith

Checks if the string ends with the given suffix; returns True or False. lstr.endswith(suffix) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.endswith('hfs'))

Result:

<10> lower

Converts all uppercase characters in the string to lowercase. lstr.lower() Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.lower())

Result:

<11> upper

Converts all lowercase characters in the string to uppercase. lstr.upper() Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.upper())

Result:

<12> strip

Removes leading and trailing whitespace characters from the string.

a = "
\t itcast \t
"
print(a.strip())  # 'itcast'

<13> rfind

Like find() but searches from the right end of the string. lstr.rfind(str, start=0, end=len(lstr)) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.rfind('eijing'))

Result:

<14> rindex

Like index() but searches from the right end. lstr.rindex(str, start=0, end=len(lstr)) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.rindex('eijing'))

Result:

<15> partition

Splits the string into a three‑tuple (head, separator, tail) based on the first occurrence of the separator. lstr.partition(sep) Example:

lstr = 'welcome to Beijing Museumitcpps  fdsfs'
print(lstr.partition('eijing'))

Result:

<16> join

Concatenates an iterable of strings using the specified separator. separator.join(iterable) Example:

str = '233'
li = ["my", "name", "is", "LY"]
print(str.join(li))

Result:

2. Summary

The article provides a detailed walkthrough of fundamental Python string operations, covering slicing, indexing, and a wide range of built‑in methods, while also presenting common pitfalls and practical solutions to help readers deepen their Python programming skills.

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.

StringTutorialexamplescodemethods
Python Crawling & Data Mining
Written by

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!

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.