2 min read•june 24, 2024
Strings 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.
True
or False
)
==
checks if two strings are equal ("hello" == "hello"
returns True
)[!=](https://www.fiveableKeyTerm:!=)
checks if two strings are not equal ("hello" != "world"
returns True
)<
, [>](https://www.fiveableKeyTerm:>)
, [<=](https://www.fiveableKeyTerm:<=)
, >=
compare strings lexicographically based on ASCII or values ("apple" < "banana"
returns True
)in
returns True
if a substring is found in a string ("lo" in "hello"
returns True
)[not in](https://www.fiveableKeyTerm:not_in)
returns True
if a substring is not found in a string ("abc" not in "hello"
returns True
)"Hello" == "hello"
returns False
)[lower()](https://www.fiveableKeyTerm:lower())
converts all characters in a string to lowercase ("Hello".lower()
returns "hello"
)[upper()](https://www.fiveableKeyTerm:upper())
converts all characters in a string to uppercase ("Hello".upper()
returns "HELLO"
)[capitalize()](https://www.fiveableKeyTerm:capitalize())
converts the first character of a string to uppercase and the rest to lowercase ("hello world".capitalize()
returns "Hello world"
)[title()](https://www.fiveableKeyTerm:title())
converts the first character of each word in a string to uppercase and the rest to lowercase ("hello world".title()
returns "Hello World"
)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"
+
operator ("Hello" + " " + "world"
returns "Hello world"
)[len()](https://www.fiveableKeyTerm:len())
returns the length of a string (len("Hello")
returns 5
)[str](https://www.fiveableKeyTerm:str)()
converts an object to its string representation (str(42)
returns "42"
)[ord()](https://www.fiveableKeyTerm:ord())
returns the Unicode code point of a single-character string (ord("A")
returns 65
)[chr()](https://www.fiveableKeyTerm:chr())
returns the character represented by a Unicode code point (chr(65)
returns "A"
)"Hello"[0]
returns "H"
)\n
for newline, \t
for tab).format()
method, and %
operator