Strings are sequences of characters, enclosed in single or double quotes. Mastering string manipulation is crucial for coding interviews.
str1 = "Hello"
str2 = 'World'
"""This is multiline string"""
str[0]
for first characterstr[-1]
for last characterstr[1:4]
gives characters from index 1 to 3len(s)
: Length of the strings.upper()
, s.lower()
, s.title()
, s.capitalize()
s.strip()
: Removes whitespacess.replace("a", "b")
: Replace characterss.find("word")
, s.index("word")
"Python" in s
: Check if substring exists+
: "Hello" + " World"
f"Hello {name}"
format()
: "{} is {}".format("Python", "awesome")
str(123)
: Convert int to stringint("123")
: Convert string to intfloat("3.14")
split()
: Split into list of wordsjoin()
: Combine list into stringstartswith()
, endswith()
isdigit()
, isalpha()
, isalnum()
count(sub)
: Count occurrencesStrings are immutable in Python. Operations return a new string instead of modifying the original.
๐ Practice daily to master these concepts!