Strings in Python are versatile tools for text manipulation. You can compare them using operators, change their case, and slice them to extract specific parts. These operations are essential for working with text data.
String manipulation techniques allow you to combine, modify, and analyze text. Understanding these concepts is crucial for tasks like data cleaning, text processing, and building user interfaces in Python programs.
String Manipulation and Comparison
String comparison with operators
- Logical operators compare strings and return boolean values (
True
orFalse
)==
checks if two strings are equal ("hello" == "hello"
returnsTrue
)!=
checks if two strings are not equal ("hello" != "world"
returnsTrue
)<
,>
,<=
,>=
compare strings lexicographically based on ASCII or Unicode values ("apple" < "banana"
returnsTrue
)
- Membership operators check if a substring exists within a string
in
returnsTrue
if a substring is found in a string ("lo" in "hello"
returnsTrue
)not in
returnsTrue
if a substring is not found in a string ("abc" not in "hello"
returnsTrue
)
- Python string comparisons are case-sensitive by default (
"Hello" == "hello"
returnsFalse
)
Changing string case
lower()
converts all characters in a string to lowercase ("Hello".lower()
returns"hello"
)upper()
converts all characters in a string to uppercase ("Hello".upper()
returns"HELLO"
)capitalize()
converts the first character of a string to uppercase and the rest to lowercase ("hello world".capitalize()
returns"Hello world"
)title()
converts the first character of each word in a string to uppercase and the rest to lowercase ("hello world".title()
returns"Hello World"
)- These are examples of string methods, which are built-in functions that operate on strings
String manipulation techniques
- String slicing extracts a portion of a string using the syntax
string[start:end:step]
start
is the starting index (inclusive), default is 0end
is the ending index (exclusive), default is the length of the stringstep
is the stride or step size, default is 1"Hello"[1:4]
returns"ell"
- String concatenation (concatenation) combines strings using the
+
operator ("Hello" + " " + "world"
returns"Hello world"
) - Built-in functions for string manipulation
len()
returns the length of a string (len("Hello")
returns5
)str()
converts an object to its string representation (str(42)
returns"42"
)ord()
returns the Unicode code point of a single-character string (ord("A")
returns65
)chr()
returns the character represented by a Unicode code point (chr(65)
returns"A"
)
Additional String Concepts
- Immutability: Strings in Python are immutable, meaning their contents cannot be changed after creation
- Indexing: Accessing individual characters in a string using square brackets and integer indices (e.g.,
"Hello"[0]
returns"H"
) - Escape sequences: Special character combinations used to represent non-printable or special characters (e.g.,
\n
for newline,\t
for tab) - String formatting: Various methods to format strings, including f-strings,
.format()
method, and%
operator