Skip to main content

Python Strings: A Beginner's Guide to Manipulating Textual Data

One of the most common tasks in any programming language is working with strings. In this blog post, we will explore the basics of Python strings, including how to create and manipulate them.

Creating Strings

In Python, a string is a sequence of characters enclosed in single quotes (' ') or double quotes (" "). Here are some examples:

# Single quotes
string1 = 'Hello, World!'

# Double quotes
string2 = "This is a string."

In addition to single and double quotes, you can also use triple quotes (''' ''') or (""" """) to create multiline strings.

# Multiline string
string3 = '''
This is a
multiline
string.
'''

Accessing Characters in a String

Once you have created a string, you can access individual characters using indexing. In Python, indexing starts at 0. Here is an example:

# Accessing characters in a string
string = "Python Strings"
print(string[0]) # Output: P
print(string[1]) # Output: y
print(string[2]) # Output: t

You can also use negative indexing to access characters from the end of the string. Here is an example:

# Negative indexing
string = "Python Strings"
print(string[-1]) # Output: s
print(string[-2]) # Output: g
print(string[-3]) # Output: n

String Slicing

In addition to accessing individual characters, you can also slice a string to access a range of characters. Here is an example:

# String slicing
string = "Python Strings"
print(string[0:6]) # Output: Python

In this example, the slice [0:6] means to start at index 0 and go up to, but not including, index 6. You can also use negative indices in slicing. Here is an example:

# Negative string slicing
string = "Python Strings"
print(string[-6:]) # Output: Strings

In this example, the slice [-6:] means to start at the sixth character from the end of the string and go to the end of the string.

String Concatenation

You can concatenate (combine) two or more strings using the + operator. Here is an example:

# String concatenation
string1 = "Hello"
string2 = "World"
string3 = string1 + " " + string2
print(string3) # Output: Hello World

Format Strings

As we learned before in the Python Variables tutorial, we cannot combine strings with other types (i.e. numbers):

Error
year = 2023
txt = "Hello World, it is " + year
print(txt)

To combine string with numbers we can use the format() method. The format() method takes the passed arguments, formats them, and places them in the string where the {} placeholders are:

year = 2023
txt = "Hello World, it is {}"
print(txt.format(year)) #outputs: Hello World, it is 2023

When dealing with multiple arguments, the format() method places them in their respective placeholders:

day = 8
month = 3
year = 2023
txt = "Hello it is day {}, month {} and year {}"
print(txt.format(day, month, year)) #outputs: Hello it is day 8, month 3 and year 2023

You can also make sure the arguments are placed in the right placeholders by using indexing {0}:

day = 8
month = 3
year = 2023
txt = "Hello it is day {1}, month {2} and year {0}"
print(txt.format(year, day, month)) #outputs: Hello it is day 8, month 3 and year 2023

String Methods

Python has many built-in string methods that you can use to manipulate strings. Here are a few examples:

# String methods
string = "Python Strings"
print(string.upper()) # Output: PYTHON STRINGS
print(string.lower()) # Output: python strings
print(string.replace("Python", "Java")) # Output: Java Strings

In this example, the upper() method converts all characters in the string to uppercase, the lower() method converts all characters to lowercase, and the replace() method replaces all occurrences of "Python" with "Java".

Escape Character

There are illegal characters in a string, for example using double quotes inside a string that is surrounded by double quotes. To escape characters that are illegal in a string, you can use an escape character which is a backslash \ followed by the character you want to insert.

Error

You will get an error if you use double quotes inside a string that is surrounded by double quotes:

txt = "Hello World this is "The Innohive" blog"

Instead what you should, do is use the escape character to insert the double strings:

txt = "Hello World this is \"The Innohive\" blog"
CodeResult
\'Single quote
\\Backslash
\nNew line
\rCarriage return
\tTab
\bBackspace
\fForm feed
\oooOctal value
\xhhHex value

Conclusion

In this blog post, we covered the basics of Python strings. We learned how to create strings, access individual characters, slice strings, concatenate strings, and use string methods. With this knowledge, you can start working with strings in Python and take your programming skills to the next level.