Skip to content

Modify Strings

Introduction

Python is a versatile programming language that is widely used in various industries. One of its most powerful features is strings. Strings are a sequence of characters that are enclosed in quotes. In this article, we will explore how to modify strings in Python.

Important: Python strings are immutable, meaning these operations do not change the original string in place. Instead, they return a new string that you can assign to a variable.

String Concatenation

String concatenation is the process of combining two or more strings into a single string. In Python, you can concatenate strings using the + operator. For example, let's say we have two strings:

python
str1 = "Hello"
str2 = "World"

We can concatenate these strings using the + operator:

Concatenate strings in Python

python
result = str1 + str2
print(result)

Output: HelloWorld

String Repetition

String repetition is the process of repeating a string a certain number of times. In Python, you can repeat a string using the * operator. For example, let's say we have a string:

python
str1 = "Hello"

We can repeat this string using the * operator:

Repeat a string in Python

python
result = str1 * 3
print(result)

Output: HelloHelloHello

Changing Case

Python provides several methods to change the case of a string. The two most commonly used methods are the `upper()` and `lower()` methods. The `upper()` method converts all characters in a string to uppercase, while the `lower()` method converts all characters in a string to lowercase.

Convert a string to uppercase or lowercase in Python

python
str1 = "Hello, World!"
print(str1.upper())  # Output: HELLO, WORLD!
print(str1.lower())  # Output: hello, world!

Slicing Strings

Slicing is the process of extracting a portion of a string. In Python, you can slice a string using the square bracket notation. For example, let's say we have a string:

python
str1 = "Hello, World!"

We can slice this string using the square bracket notation:

Slice strings in Python

python
print(str1[0:5])  # Output: Hello
print(str1[7:])   # Output: World!

Replacing Substrings

Replacing substrings is the process of replacing a portion of a string with another string. In Python, you can replace substrings using the `replace()` method. For example, let's say we have a string:

python
str1 = "Hello, World!"

We can replace a substring in this string using the `replace()` method:

Replace a part of a string in Python

python
str1 = "Hello, World!"
print(str1.replace("World", "Python"))  # Output: Hello, Python!

Conclusion

In this article, we have explored various ways to modify strings in Python, including string concatenation, string repetition, changing case, slicing strings, and replacing substrings. We hope that this article has been helpful in your understanding of how to work with strings in Python.

Practice

What are the methods available in Python for modifying strings?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.