Master Python String Manipulation: 11 Essential Techniques
This guide walks you through eleven practical Python string operations—including case conversion, whitespace trimming, numeric checks, character replacement, splitting, prefix/suffix testing, formatting, encoding, joining, and binary conversion—each illustrated with clear code examples you can run instantly.
This article presents a collection of common Python string manipulation techniques, each accompanied by concise code examples.
1. Convert string case
value = "wangdianchao"
# Convert to uppercase
big_value = value.upper()
print(big_value)
# Convert to lowercase
small_value = big_value.lower()
print(small_value)2. Check if input can be converted to a number
num = input("Enter content:")
# Determine if the string represents a digit
flag = num.isdigit()
print(flag)3. Remove spaces from a string
user = input("Please enter username:")
# Remove trailing spaces
new_user = user.rstrip()
print(new_user)
# Remove leading spaces
new_user = user.lstrip()
print(new_user)
# Remove spaces on both sides (or newlines)
new_user = user.strip()
print(new_user)4. Replace characters in a string
message = input("Please enter information:")
# Replace "大爷" with "**"
data = message.replace('大爷', '**')
print(data)
# Replace only the first occurrence
data = message.replace('大爷', '**', 1)
print(data)
# Replace the first two occurrences
data = message.replace('大爷', '**', 2)
print(data)5. Split a string
message = "当清晨的一缕阳光透过窗帘上的空隙映照在沉睡的脸庞时,微微张开的双眼朦胧地注视着周遭的一切,新的一天悄然而至。"
# Split by comma
data = message.split(',')
print(data)
# Split once
data = message.split(',', 1)
print(data)
# Split from the right once
data = message.rsplit(',', 1)
print(data)6. Check if a string starts with a substring
str_val = "this is string example....wow!!!"
print(str_val.startswith('this'))
# Using start and end positions
print(str_val.startswith('is', 2, 4))
print(str_val.startswith('this', 2, 4))7. Check if a string ends with a substring
str_val = "this is string example....wow!!!"
suffix = "wow!!!"
print(str_val.endswith(suffix))
print(str_val.endswith(suffix, 20))
suffix = "is"
print(str_val.endswith(suffix, 2, 4))
print(str_val.endswith(suffix, 2, 6))8. Format a string
template = "Website: {name}, URL: {url}"
print(template.format(name="Baidu", url="www.baidu.com"))9. Change string encoding
str_val = "this is string example....wow!!!"
print(str_val.encode('utf-8'))10. Join elements of a sequence into a string
sep = "-"
seq = ("a", "b", "c") # Elements must be strings
print(sep.join(seq))11. Convert between string and binary
data = '王佃超'
# Convert string to binary
binary_data = data.encode('utf-8')
# Convert binary back to string
text = binary_data.decode('utf-8')
print(text)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.
