What is a 'string' in Python?

A foundational guide to the string (str) data type in Python. Learn how strings are used to represent text, their immutable nature, and some of the most common operations you can perform on them.

In Python, as in most programming languages, a string is a sequence of characters used to represent text. It's one of the most fundamental and commonly used data types. Whenever you work with text—a person's name, a message, a line from a file—you are working with a string.

In Python, the string type is called str.

Creating Strings

Python is flexible in how you can create strings. You can enclose them in either single quotes (') or double quotes ("). There is no functional difference between them; you can choose the one that is more convenient.

# These are all valid strings
my_name = "Alice"
my_message = 'Hello, World!'

# You can use one type of quote to contain the other
quote = "He said, 'Hello!'"

For multi-line strings, you can use triple quotes (''' or """).

multi_line_string = """
This is a string
that spans multiple
lines.
"""

Strings are Immutable

This is a critical concept. In Python, strings are immutable, which means that once a string is created, it cannot be changed. Any function or method that appears to modify a string actually returns a new string.

my_string = "hello"

# This does not change the original string.
# It creates a new string, 'HELLO', and the 'upper_string' variable points to it.
upper_string = my_string.upper()

print(my_string)    # 'hello'
print(upper_string) # 'HELLO'

Common String Operations

Concatenation

You can combine strings using the + operator.

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

Accessing Characters (Indexing)

Since a string is a sequence, you can access individual characters using a zero-based index.

my_string = "Python"
first_char = my_string[0] # 'P'
last_char = my_string[-1] # 'n' (negative indexing from the end)

Slicing

You can extract a substring using slice notation [start:stop].

my_string = "Python"
sub = my_string[1:4] # 'yth' (from index 1 up to, but not including, index 4)

Useful String Methods

Strings come with a huge number of useful methods. Here are just a few:

  • upper() and lower(): Return a new string in all uppercase or all lowercase.
  • strip(): Returns a new string with leading and trailing whitespace removed.
  • startswith(prefix) and endswith(suffix): Check if the string starts or ends with a specific substring.
  • find(substring): Returns the index of the first occurrence of a substring, or -1 if it's not found.
  • replace(old, new): Returns a new string where all occurrences of the old substring are replaced with the new one.
  • split(separator): Splits the string into a list of substrings based on a separator.
csv_line = "apple,banana,cherry"
fruits = csv_line.split(',') # ['apple', 'banana', 'cherry']

Formatting Strings

While you can build strings with +, it's often better to use a formatting method. In modern Python (3.6+), f-strings are the preferred way.

name = "Alice"
age = 30

message = f"{name} is {age} years old."
# Output: Alice is 30 years old.

Conclusion

The string is a fundamental building block of almost every Python program. By understanding its immutable nature and becoming familiar with its rich set of methods for manipulation and formatting, you can handle any text-processing task with ease. It's a versatile and powerful data type that you will use constantly in your Python journey.