Introduction to Strings in Python
Strings in Python are one of the fundamental data types used to represent and manipulate text. They are immutable, meaning that once a string is created, it cannot be modified. Instead, new strings are created for any modifications.
Defining Strings
Strings can be defined using single quotes (‘), double quotes (“), or triple quotes (”’ or “””).
Single and Double Quotes
You can use either single or double quotes to define a string. The choice between the two depends on whether you need to include quotes within the string itself.
single_quote_string = 'Hello, world!' double_quote_string = "Hello, world!"
If your string contains single quotes, use double quotes to enclose the string, and vice versa.
quote1 = "It's a beautiful day!" # Uses double quotes to include a single quote quote2 = 'He said, "Hello!"' # Uses single quotes to include double quotes
Triple Quotes
Triple quotes are used for multi-line strings or long text blocks without needing to escape new lines.
multiline_string = """This is a string that spans multiple lines. No need for escape characters!""" print(multiline_string)
Types of Strings
Unicode Strings
Since Python 3, all strings are Unicode by default, allowing you to handle a wide range of characters from different languages and scripts.
unicode_string = "こんにちは世界" # "Hello, world" in Japanese print(unicode_string)
Raw Strings
Raw strings are used to treat backslashes as literal characters and are useful for paths or regular expressions.
raw_string = r"C:\Users\Name\Documents\file.txt" print(raw_string) # Output: C:\Users\Name\Documents\file.txt
Basic Operations
Concatenation
Strings can be concatenated using the + operator.
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name print(full_name) # Output: John Doe
Repetition
Strings can be repeated a specified number of times using the * operator.
echo = "Hello! " * 3 print(echo) # Output: Hello! Hello! Hello!
Accessing Characters
Strings are indexed, which allows you to access individual characters using their index. Indexing starts from 0.
text = "Python" print(text[0]) # Output: P print(text[5]) # Output: n print(text[-1]) # Output: n print(text[-2]) # Output: o
Slicing
Slicing allows you to create substrings by specifying start and end indices.
text = "Hello, world!" substring = text[0:5] # From index 0 to index 5 (exclusive) print(substring) # Output: Hello
Slicing with Unspecified Indices
- text[:5] : Takes the first 5 characters.
- text[7:] : Takes the characters from index 7 to the end.
- text[:] : Takes the whole string.
print(text[:5]) # Output: Hello print(text[7:]) # Output: world! print(text[:]) # Output: Hello, world!
Slicing with Step
The step parameter in slicing specifies the increment between indices.
text = "Python" print(text[::2]) # Output: Pto print(text[1::2]) # Output: yhn print(text[::-1]) # Output: nohtyP (reverses the string)
String Methods
Python strings come with a variety of built-in methods for manipulation and analysis.
lower() and .upper()
- .lower() : Converts all characters to lowercase.
- .upper() : Converts all characters to uppercase.
text = "Hello World" print(text.lower()) # Output: hello world print(text.upper()) # Output: HELLO WORLD
strip()
Removes whitespace from the beginning and end of a string. Use .lstrip() to remove leading whitespace and .rstrip() to remove trailing whitespace.
text = " trim me " print(text.strip()) # Output: trim me print(text.lstrip()) # Output: trim me print(text.rstrip()) # Output: trim me
replace(old, new)
Replaces all occurrences of old with new.
text = "Hello, world!" print(text.replace("world", "Python")) # Output: Hello, Python!
find(sub) and .rfind(sub)
- .find(sub) : Finds the first occurrence of sub. Returns -1 if not found.
- .rfind(sub) : Finds the last occurrence of sub.
text = "Find the position of the word 'the'." print(text.find("the")) # Output: 15 print(text.rfind("the")) # Output: 24
split(delimiter)
Splits the string into a list using the specified delimiter.
text = "one,two,three" print(text.split(",")) # Output: ['one', 'two', 'three']
join(iterable)
Joins elements of an iterable (like a list) into a single string, separating them by the string on which .join() is called.
words = ['Hello', 'world'] sentence = ' '.join(words) print(sentence) # Output: Hello world
startswith(prefix) and .endswith(suffix)
Checks if the string starts or ends with prefix or suffix.
text = "Hello, world!" print(text.startswith("Hello")) # Output: True print(text.endswith("world!")) # Output: True
String Formatting
- Formatting allows you to embed variables and expressions within strings.
format()
The format() method allows for inserting variables into a string with format specifiers.
name = "Alice" age = 30 formatted_string = "Name: {}, Age: {}".format(name, age) print(formatted_string) # Output: Name: Alice, Age: 30
f-strings (Python 3.6+)
f-strings (formatted string literals) provide a concise way to include expressions inside strings. Expressions inside curly braces {} are evaluated at runtime.
name = "Alice" age = 30 f_string = f"Name: {name}, Age: {age}" print(f_string) # Output: Name: Alice, Age: 30
str() and repr()
- str() : Returns a user-friendly string representation of the object.
- repr() : Returns a string that can be used to recreate the object.
text = "Hello, world!" print(str(text)) # Output: Hello, world! print(repr(text)) # Output: 'Hello, world!'