Skip to content

Mastering Python Strings: Basics, Manipulation, and Real-World Examples

Updated: at 12:39 PM

Strings are one of the most commonly used data types in Python. This in-depth guide will provide a solid foundation in working with strings in Python 3, including creation, manipulation, formatting, and real-world use cases.

Table of Contents

Open Table of Contents

What is a String?

A string in Python is a sequence of Unicode characters. Strings are surrounded by either single quotes (’), double quotes (”), or triple quotes (""" or ''').

For example:

# Single quotes
'Hello'

# Double quotes
"World"

# Triple quotes
'''This is a multi-line string'''

Strings are immutable in Python - once created, the elements inside a string can’t be changed. When you perform operations on strings, like concatenation, Python creates a new string with the results.

Creating Strings

There are a few different ways to create strings in Python:

Using Quotes

You can use single, double, or triple quotes to create strings, as shown in the examples above. Triple quotes are used for multiline strings spanning multiple lines.

String Literals

You can create raw string literals by prefixing the string with r or R. This will interpret backslash escapes directly as characters in the string, rather than escape sequences.

For example:

print(r'Hello\nWorld') # Prints Hello\nWorld
print('Hello\nWorld') # Prints two lines

Bytes and Bytearrays

While strings contain Unicode characters, bytes and bytearrays contain raw binary data. They are used when working with binary files, network streams, or serialization:

# Bytes
b'Hello'

# Bytearray
bytearray(b'Hello')

String Constructors

The str() and bytes() constructors can be used to create strings from objects:

str(5) # '5'
bytes(5) # b'5'

For custom classes, str() will call the str() method to get a string representation of the object.

String Manipulation

There are many ways to manipulate and modify strings in Python. Here are some common techniques:

Concatenation

You can combine strings using the + operator:

first_name = 'John'
last_name = 'Doe'
full_name = first_name + ' ' + last_name # 'John Doe'

To repeat a string multiple times, you can multiply it:

'Hello ' * 3 # 'Hello Hello Hello'

Slicing Strings

You can extract substrings using slice notation:

name = 'John Doe'

name[0] # 'J'
name[0:4] # 'John'
name[:4] # 'John'
name[4:] # 'Doe'

Slicing allows grabbing parts of a string by index. The character at index 4 (space) is not included in the slice.

Formatting Strings

Python has powerful formatting with the % and {} operators:

'Hello %s' % 'John' # 'Hello John'

'Hello {name}'.format(name='John') # 'Hello John'

This allows substituting values into placeholders in the string.

Escape Characters

You can insert newlines, tabs, and other escapes with :

'Hello\nWorld' # Prints two lines
'\tHello' # Indents 'Hello'

Common escapes are \, ’, ”, \n, and \t.

Built-in String Methods

Python has many string methods for transforming and manipulating strings:

len()

Returns the length of the string:

len('Hello') # 5

lower() / upper()

Converts the string to lower or uppercase:

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

strip()

Removes leading and trailing whitespace:

' Hello '.strip() # 'Hello'

split()

Splits the string on a delimiter into a list:

'Hello,World'.split(',') # ['Hello', 'World']

replace()

Replaces occurrences of a substring:

'Hello'.replace('l', 'w') # 'Hewwo'

startswith() / endswith()

Checks if the string starts or ends with a substring:

'Hello'.startswith('He') # True
'Hello'.endswith('lo') # True

These are just a few of the commonly used string methods. Refer to the Python docs for a complete list.

String Formatting

Python has powerful formatting capabilities that allow injecting data into strings.

%-Format Strings

This style uses %s, %d, and other placeholders to inject values:

name = 'John'
'Hello %s' % name # 'Hello John'

age = 25
'I am %d years old' % age # 'I am 25 years old'

You can also format numbers and floats:

pi = 3.14159
'Pi is %.2f' % pi # 'Pi is 3.14'

str.format()

This method uses curly brace {} placeholders and the .format() method:

'Hello {name}'.format(name='John')

'Pi is {pi:1.2f}'.format(pi=3.14159) # 'Pi is 3.14'

Variables can be directly referenced inside the placeholders.

f-strings (Python 3.6+)

f-strings provide an easy way to embed expressions in string literals:

name = 'John'
f'Hello {name}' # 'Hello John'

import math
f'2π is {math.pi:.3f}' # '2π is 3.142'

This syntax removes the need to break strings over multiple lines for formatting.

Real-World Example: Parsing CSV Data

Here is an example demonstrating how strings and string methods can parse CSV data:

import csv

with open('data.csv') as f:
  reader = csv.reader(f)
  for row in reader:
    print(row[0].upper())

    values = row[0].split(',')
    total = len(values)
    print(f'Total values: {total}')

This parses a CSV file, uppercases the first column, and counts the number of elements in it using split(). The csv module handles opening and reading the file.

String methods like split() and upper() enable clean parsing and manipulation of textual CSV data.

Case Study: Developing a URL Shortener Service

Let’s go through a hypothetical case study for developing a URL shortener service like Bitly using Python and strings.

Our service will:

Here is one way we could implement short code generation:

import random
import string

class URLShortener:

  def __init__(self):
    self.short_urls = {}

  def shorten(self, url):
    code = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
    self.short_urls[code] = url
    return f'https://domain.com/{code}'

We randomly generate a 6 character alphanumeric string code and store it mapped to the long URL.

For a real-world service, this would require expanding the functionality - for example, integrating a database for persistent storage instead of the in-memory dict, handling redirects, implementing an HTTP interface, analytics, rate limiting, etc.

But it demonstrates core string principles like generating random strings and storing string mappings that could serve as the foundation for building out a URL shortening web service using Python.

Conclusion

In summary, this guide covered:

Python’s extensive string handling features provide all the tools necessary for processing textual data. Mastering Python strings enables writing robust programs and scripts that work with files, networks, web services, and more.

For more information, refer to the official Python 3 documentation on strings: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str

There are always more techniques to learn for text processing, like regular expressions, internationalization, and custom string classes. With foundational string skills, Python developers can continue building their textual manipulation capabilities and create more powerful applications.