Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is str in Python

Understanding str in Python

When you're starting out in the world of programming, you'll come across various types of data that you can work with. In Python, one of the most basic and frequently used data types is the string, often abbreviated as str. But what exactly is a str? Let's break it down.

What is a String (str)?

Imagine you have a pearl necklace. Each pearl can represent a character, like a letter, a number, or a symbol. A string in Python is similar to this necklace; it's a sequence of characters strung together to form text. In Python, anything enclosed in quotes (single ', double ", or triple ''' or """) is considered a string.

greeting = "Hello, World!"
print(greeting)  # This will output: Hello, World!

In the example above, "Hello, World!" is a string. We know it's a string because it's wrapped in double quotes. You could also use single quotes like 'Hello, World!'.

Creating and Using Strings

Creating a string is as simple as assigning some text into a variable. Here's how you can create strings using different types of quotes:

single_quoted_str = 'This is a single-quoted string.'
double_quoted_str = "This is a double-quoted string."
triple_quoted_str = '''This is a triple-quoted string, which can
span multiple lines.'''

You can use strings to store names, messages, or any other textual information. Python treats single and double quotes the same, but triple quotes allow you to create strings that span multiple lines.

String Concatenation

What if you want to combine two strings together? This is called concatenation, and you can think of it like linking two pearl necklaces end-to-end to make a longer necklace.

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)  # This will output: John Doe

In the code above, we've combined first_name and last_name with a space in between to create full_name.

Strings Are Immutable

In Python, strings are immutable. This means once you create a string, you cannot change it. It's like once you've tied the knot on your pearl necklace, you can't swap out a pearl without making a whole new necklace.

If you try to change a string, you'll actually create a new string instead of modifying the original one:

original_string = "cat"
# Let's say we want to change 'c' to 'b' to make it "bat"
# This is how you might think to do it:
original_string[0] = "b"  # This will raise an error!

Instead, you have to create a new string:

original_string = "cat"
# Correct way to change 'c' to 'b'
new_string = "b" + original_string[1:]
print(new_string)  # This will output: bat

Accessing Characters and Substrings

You can access individual characters in a string by their position, known as indexing. If you wanted to get the first pearl from your necklace, you'd just reach for the one at the very beginning. In Python, you use square brackets [] to do this:

alphabet = "abcdefg"
first_letter = alphabet[0]
print(first_letter)  # This will output: a

Python uses zero-based indexing, which means the first character is at index 0, the second is at index 1, and so on.

You can also grab a range of characters, known as a substring, by specifying a start and an end index:

substring = alphabet[1:4]  # This will include characters at index 1, 2, and 3
print(substring)  # This will output: bcd

String Methods

Python provides many built-in methods that you can use on strings. These methods are like tools you can use to work with your strings. Here are a few examples:

  • .upper() converts all the characters in a string to uppercase.
  • .lower() converts all the characters in a string to lowercase.
  • .replace(old, new) replaces all occurrences of the old substring with the new substring.

Let's see these methods in action:

message = "Python is fun!"
upper_message = message.upper()
print(upper_message)  # This will output: PYTHON IS FUN!

lower_message = message.lower()
print(lower_message)  # This will output: python is fun!

replaced_message = message.replace("fun", "awesome")
print(replaced_message)  # This will output: Python is awesome!

Formatting Strings

Sometimes you need to create a string with some variable content. For example, you might want to create a personalized welcome message for users of your program. Python provides several ways to format strings, and one of the most convenient is f-strings (introduced in Python 3.6).

F-strings allow you to embed expressions inside string literals using curly braces {}:

name = "Alice"
age = 25
welcome_message = f"Hello, {name}! You are {age} years old."
print(welcome_message)  # This will output: Hello, Alice! You are 25 years old.

Escaping Characters

What if you want to include a quote inside your string? Since quotes are used to define the start and end of a string, you need a way to tell Python that a quote should be considered as part of the string. This is done using a backslash \, known as an escape character.

quote = "He said, \"Python is amazing!\""
print(quote)  # This will output: He said, "Python is amazing!"

By using \ before the double quote inside the string, you're telling Python to treat it as a literal character, not as the end of the string.


Delving into the world of strings in Python can feel like learning a new language. But just like learning to speak through sentences, mastering strings allows you to express complex ideas (or data) in your programs. Remember, strings in Python are like necklaces of characters, immutable and versatile, ready to be manipulated and presented in countless ways. Whether you're formatting, slicing, or concatenating, each string operation you learn is another word in your programming vocabulary, bringing you one step closer to fluency in the language of Python. Keep practicing, and soon you'll be crafting eloquent "sentences" in the world of code.