Beyond numbers and logical values, a fundamental aspect of programming involves working with text. In Python, textual data is represented using strings. Think of a string as an ordered sequence of characters. Anything you enclose in quotes – letters, numbers, symbols, spaces – becomes a string.
Python is quite flexible in how you define strings. You can use either single quotes ('
) or double quotes ("
). This flexibility is useful if your string itself contains quotes.
# Using single quotes
message_single = 'Hello, Python learner!'
print(message_single)
# Using double quotes
message_double = "This also works just fine."
print(message_double)
# What if your string contains a single quote? Use double quotes to enclose it.
quote = "He said, 'Python is fun!'"
print(quote)
# What if your string contains a double quote? Use single quotes.
reply = 'She responded, "Indeed it is."'
print(reply)
What if you need a string that spans multiple lines or contains both single and double quotes without hassle? Python offers triple quotes, either '''
or """
.
# Triple double quotes for multi-line string
multi_line_doc = """This is the first line.
This is the second line.
And "quotes" or 'apostrophes' can be used freely inside."""
print(multi_line_doc)
# Triple single quotes work the same way
multi_line_alt = '''Another way to write
multi-line text,
very convenient.'''
print(multi_line_alt)
You can combine strings using the +
operator, an operation called concatenation. You can also repeat strings using the *
operator.
first_name = "Ada"
last_name = "Lovelace"
space = " "
# Concatenation
full_name = first_name + space + last_name
print(full_name) # Output: Ada Lovelace
# Repetition
separator = "-" * 10 # Repeat the hyphen 10 times
print(separator) # Output: ----------
print(first_name * 3) # Output: AdaAdaAda
Note that you can only concatenate strings with other strings. Trying to add a string to a number directly will cause an error. You'd need to convert the number to a string first, using str()
, which we'll cover soon under Type Conversion.
Since strings are sequences, you can access individual characters using their position, or index. Python uses zero-based indexing, meaning the first character is at index 0, the second at index 1, and so on.
language = "Python"
# Accessing characters by positive index
print(language[0]) # Output: P (the first character)
print(language[1]) # Output: y (the second character)
print(language[5]) # Output: n (the sixth character)
# Accessing characters using negative index (starts from the end)
print(language[-1]) # Output: n (the last character)
print(language[-2]) # Output: o (the second-to-last character)
Trying to access an index that doesn't exist (e.g., language[6]
in the example above) will result in an IndexError
.
If you need more than one character, you can extract a portion of a string, called a substring or slice. Slicing uses the syntax [start:stop:step]
.
start
: The index where the slice begins (inclusive). If omitted, defaults to 0.stop
: The index where the slice ends (exclusive). If omitted, defaults to the end of the string.step
: The amount to increment by. If omitted, defaults to 1.language = "Python"
# Get characters from index 1 up to (but not including) index 4
print(language[1:4]) # Output: yth
# Get characters from the beginning up to index 3 (exclusive)
print(language[:3]) # Output: Pyt
# Get characters from index 2 to the end
print(language[2:]) # Output: thon
# Get the whole string (less common, but shows the defaults)
print(language[:]) # Output: Python
# Get every second character
print(language[::2]) # Output: Pto
# Get the string in reverse
print(language[::-1]) # Output: nohtyP
Slicing always produces a new string and never causes an IndexError
if the indices are out of bounds; it simply returns whatever part of the string is available within the requested range.
An important characteristic of Python strings is that they are immutable. This means that once a string is created, it cannot be changed in place. Operations that seem to modify a string, like concatenation or methods we'll see next, actually create and return new strings.
greeting = "hello"
# greeting[0] = "H" # This will cause a TypeError: 'str' object does not support item assignment
# To "change" the greeting, create a new string
new_greeting = "H" + greeting[1:]
print(new_greeting) # Output: Hello
print(greeting) # Output: hello (original string is unchanged)
This immutability might seem restrictive, but it makes strings predictable and safe to use in various contexts, like dictionary keys (which you'll learn about later).
Strings come with many built-in methods that perform common operations. Methods are like functions, but they are called on an object (in this case, a string) using dot notation (.
). Here are a few useful ones:
len(string)
: Although technically a built-in function, not a method, len()
is essential for getting the number of characters in a string..lower()
/ .upper()
: Return a new string with all characters converted to lowercase or uppercase, respectively..strip()
/ .lstrip()
/ .rstrip()
: Return a new string with leading and/or trailing whitespace removed..find(substring)
: Returns the starting index of the first occurrence of substring
. Returns -1 if not found..replace(old, new)
: Returns a new string where all occurrences of old
are replaced with new
..startswith(prefix)
/ .endswith(suffix)
: Return True
or False
depending on whether the string starts or ends with the given substring..split(separator)
: Returns a list of substrings, split wherever the separator
occurs. If no separator is given, splits by whitespace.separator.join(iterable)
: Joins the elements of an iterable (like a list) into a single string, with separator
placed between elements.text = " Learning Python is Fun! "
# Length
print(len(text)) # Output: 27
# Case conversion
print(text.lower()) # Output: learning python is fun!
print(text.upper()) # Output: LEARNING PYTHON IS FUN!
# Stripping whitespace
print(text.strip()) # Output: Learning Python is Fun!
print(text.lstrip()) # Output: Learning Python is Fun!
print(text.rstrip()) # Output: Learning Python is Fun!
# Finding substrings
print(text.find("Python")) # Output: 11 (index where 'Python' starts)
print(text.find("Java")) # Output: -1 (not found)
# Replacing substrings
print(text.replace("Fun", "Awesome")) # Output: Learning Python is Awesome!
# Checking start/end (use strip first for accuracy here)
clean_text = text.strip()
print(clean_text.startswith("Learning")) # Output: True
print(clean_text.endswith("!")) # Output: True
# Splitting a string into a list
words = clean_text.split(" ")
print(words) # Output: ['Learning', 'Python', 'is', 'Fun!']
# Joining a list into a string
joined_string = "---".join(words)
print(joined_string) # Output: Learning---Python---is---Fun!
# Original string remains unchanged due to immutability
print(text) # Output: Learning Python is Fun!
Often, you need to create strings that embed the values of variables. While you can use concatenation (+
), it quickly becomes cumbersome and error-prone, especially when dealing with non-string types. Python offers better ways to format strings.
The most modern and generally preferred method is using f-strings (formatted string literals), introduced in Python 3.6. You create an f-string by prefixing the string literal with the letter f
or F
. Inside the string, you can place variables or expressions directly within curly braces {}
.
name = "Alice"
age = 30
city = "New York"
# Using f-string for clean formatting
intro_fstring = f"My name is {name}, I am {age} years old, and I live in {city}."
print(intro_fstring)
# Output: My name is Alice, I am 30 years old, and I live in New York.
# You can also include expressions inside the braces
radius = 5
area = 3.14159 * radius**2
print(f"A circle with radius {radius} has an area of {area:.2f}.")
# Output: A circle with radius 5 has an area of 31.42.
# Note: :.2f formats the float to 2 decimal places.
F-strings are readable, concise, and generally faster than older formatting methods like .format()
or the C-style %
operator, which you might encounter in older code.
Sometimes you need to include characters in a string that are difficult or impossible to type directly or that have special meaning. For example, how do you include a newline or a tab, or a literal quote character inside a string that uses the same quote type for its boundaries? This is done using escape sequences, which start with a backslash (\
).
Common escape sequences include:
\n
: Newline\t
: Tab\\
: Literal backslash\'
: Literal single quote\"
: Literal double quote# Newline
print("First line.\nSecond line.")
# Output:
# First line.
# Second line.
# Tab
print("Column1\tColumn2")
# Output: Column1 Column2
# Literal backslash
print("This is a path: C:\\Users\\Name")
# Output: This is a path: C:\Users\Name
# Literal quotes within string defined by same quotes
print('He said, \'Hello!\'') # Output: He said, 'Hello!'
print("She replied, \"Hi!\"") # Output: She replied, "Hi!"
Strings are fundamental for handling any kind of textual data, from user input and file contents to messages and labels in your programs. Mastering their creation, manipulation, and formatting is a significant step in learning Python.
© 2025 ApX Machine Learning