In Python, a String(Python Document) is an immutable sequence of characters, enclosed in either single quotes (') or double quotes ("). Strings are a fundamental data type and support various basic operations, which are crucial for handling and manipulating text in Python.

Let’s explore some of the most common operations.

text_1 = "Hello, World!"
text_2 = 'Hello, World!'

Let’s take a look at the main functions and methods on Strings.

Key functions and methods (String operations)

len(): Returns the length of a string.

len("Hello") # 5

lower() / upper(): Case conversion.

"Hello".lower() # 'hello' 
"Hello".upper() # 'HELLO'

strip(): Remove spaces on both sides.

" Hello ".strip() # 'Hello'

replace(): String substitution.

"Hello, World!".replace("World", "Python") # 'Hello, Python!'

split(): Divide a string by a specific delimiter and return it as a list.

"a,b,c".split(",") # ['a', 'b', 'c']

join(): Merge lists into strings.

",".join(['a', 'b', 'c']) # 'a,b,c'

format() / f-string: String formatting.

"Hello, {}!".format("World") # 'Hello, World!' 
name = "World" f"Hello, {name}!" # 'Hello, World!'

isalnum() / isalpha() / isdigit(): Checking the String attribute.

"123".isdigit() # True 
"abc".isalpha() # True 
"abc123".isalnum() # True

Now that we’ve covered the basic string operations, let’s look at some code examples that demonstrate how strings are used in real applications. You don’t need to memorize everything—just knowing these functions exist and where to find them when needed is enough.

I’ll dive deeper into handling sentences and more advanced string operations in another post, so stay tuned for that!

String operations Code

first = "A"
second = "B"
full = first + " " + second
print(full)  # "A B"

repeat_str = "Ha " * 3
print(repeat_str)  # "Ha Ha Ha"

# slice
print(_str[7:12])  # "name"
print(_str[:5])  # " Hi m"
print(_str[7:])  # "name is John "

# Capital
print(_str.upper())  # " HI MY NAME IS JOHN "
print(_str.lower())  # " hi my name is john "
print(_str.capitalize())  # " hi my name is john "
print(_str.title())  # " Hi My Name Is John "

print(_str.strip())  # "Hi my name is John"
print(_str.lstrip())  # "Hi my name is John "
print(_str.rstrip())  # " Hi my name is John"

# find, replace
print(_str.find("John"))  # 15
print(_str.find("World"))  # -1
print(_str.replace("John", "Jang"))  # " Hi my name is Jang "

# f-strings (Python 3.6+)
name = "John"
age = 1
intro = f"Hi~ My name is {name}, I am {age} years old."
print(intro)  # "MHi~ My name is John, I am 1 years old."

# str.format()
intro_format = "Hi~ My name is {}, I am {} years old.".format(name, age)
print(intro_format)  # "Hi~ My name is John, I am 1 years old."

# %
intro_percent = "Hi~ My name is %s, I am %d years old." % (name, age)
print(intro_percent)  # "Hi~ My name is John, I am 1 years old."

# Split
sentence = "This is a sentence."
words = sentence.split()
print(words)  # ['This', 'is', 'a', 'sentence.']

# join
joined_sentence = ' '.join(words)
print(joined_sentence)  # "This is a sentence."

#find() / index()

"hello".find("l") # 2 
"Hello".index("l") # 2

#startswith() / endswith()

"Hello".startswith("He") # True 
"Hello".endswith("o") # True

Output

A B
Ha Ha Ha 
20
H
n
name 
 Hi m
name is John 
 HI MY NAME IS JOHN 
 hi my name is john 
 hi my name is john 
 Hi My Name Is John 
Hi my name is John
Hi my name is John 
 Hi my name is John
15
-1
 Hi my name is Jang 
Hi~ My name is John, I am 1 years old.
Hi~ My name is John, I am 1 years old.
Hi~ My name is John, I am 1 years old.
['This', 'is', 'a', 'sentence.']
This is a sentence.

Using strings in Python is straightforward and convenient. When I was working on system programming, I didn’t use string data structures much—mostly for generating logs or extracting system information.

Nowadays, however, I rely on strings a lot more, especially when extracting and summarizing information for daily tasks or reports. Recently, with the increasing use of LLMs (Large Language Models) and interacting with data through RSS feeds or APIs, string handling has become a regular part of my workflow.

When I think back to working with char in C 20 years ago, Python’s string capabilities feel incredibly convenient. While there might be a slight drop in speed compared to lower-level programming, modern advancements make it negligible for most applications. Unless you’re processing extremely large text data, like in LLM applications, the trade-off is well worth the ease of use and functionality Python provides.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *