Python strings are a fundamental data type used to represent text in Python. Strings are sequences of characters, and they are immutable, meaning once a string is created, it cannot be changed.
'
) or double quotes ("
):
s1 = 'Hello, World!'
s2 = "Hello, World!"
s = "hello"
s[0] = "H" # This will raise a TypeError
s = "Hello" # This creates a new string
Concatenation: Combine strings using the +
operator.
s = "Hello" + " " + "World"
Repetition: Repeat a string using the *
operator.
s = "Hi" * 3 # Output: 'HiHiHi'
Length: Get the length of a string using len()
:
length = len("Hello") # Output: 5
Indexing: Access individual characters using indices. Python uses zero-based indexing:
s = "Hello"
first_char = s[0] # Output: 'H'
last_char = s[-1] # Output: 'o'
Slicing: Extract a substring using slice notation s[start:end:step]
:
s = "Hello"
sub = s[1:4] # Output: 'ell'
reverse = s[::-1] # Output: 'olleH'
Python provides numerous built-in methods to manipulate and analyze strings:
str.upper()
/ str.lower()
: Convert to uppercase or lowercase:
"hello".upper() # Output: 'HELLO'
"HELLO".lower() # Output: 'hello'
str.capitalize()
/ str.title()
: Capitalize the first character or title case each word:
"hello world".capitalize() # Output: 'Hello world'
"hello world".title() # Output: 'Hello World'
str.strip()
/ str.lstrip()
/ str.rstrip()
: Remove whitespace from the ends:
" hello ".strip() # Output: 'hello'
str.split()
/ str.join()
: Split a string into a list or join a list into a string:
"a,b,c".split(",") # Output: ['a', 'b', 'c']
",".join(['a', 'b', 'c']) # Output: 'a,b,c'
str.replace(old, new)
: Replace occurrences of a substring:
"hello".replace("l", "y") # Output: 'heyyo'
str.find()
/ str.index()
: Find the position of a substring:
"hello".find("e") # Output: 1
"hello".index("l") # Output: 2
str.startswith()
/ str.endswith()
: Check if a string starts or ends with a given substring:
"hello".startswith("he") # Output: True
"hello".endswith("lo") # Output: True
Python provides multiple ways to format strings:
Old-style formatting (%
):
"Hello, %s!" % "World" # Output: 'Hello, World!'
str.format()
method:
"Hello, {}!".format("World") # Output: 'Hello, World!'
f-strings (formatted string literals): Introduced in Python 3.6, f-strings provide a concise and readable way to embed expressions inside string literals:
name = "World"
f"Hello, {name}!" # Output: 'Hello, World!'
\
) is used to escape characters in a string, allowing special characters to be included:
s = "He said, "Hello!""
r
or R
makes it a raw string, where escape sequences are not processed:
s = r"C:\new_folder\test" # No need to escape backslashes
s = "hello"
b = s.encode('utf-8') # Output: b'hello'
decoded = b.decode('utf-8') # Output: 'hello'
==
, !=
, <
, >
, etc.), which compare the lexicographical order based on Unicode values:
"apple" < "banana" # Output: True
Checking substrings: Use in
to check if a substring exists within a string:
"ell" in "Hello" # Output: True
String reversal: Reverse a string using slicing:
reversed_string = "Hello"[::-1] # Output: 'olleH'
re
module:
import re
match = re.search(r'\d+', 'abc123')
if match:
print(match.group()) # Output: '123'
+
can be inefficient because strings are immutable. Instead, use str.join()
or a list to accumulate strings and then join them at the end:
# Inefficient
s = ""
for i in range(1000):
s += str(i)
# Efficient
s = []
for i in range(1000):
s.append(str(i))
s = ''.join(s)
end
index in slicing is exclusive.str()
: Convert an object to a string.ord()
: Convert a character to its Unicode code point.chr()
: Convert a Unicode code point to a character.This overview covers most of the key concepts and operations related to Python strings. Whether you're handling simple text processing or complex string manipulation, understanding these aspects will help you work efficiently with strings in Python.