1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
string
A sequence of characters inside quotes (” “ or ’ ’).
Can strings contain spaces, digits, or special characters?
Yes, they can contain all of those.
Are strings mutable or immutable in Python?
| Immutable (cannot be changed directly, but new strings can be created).
How do you access the first character of a string?
string[0].
How do you access the last character of a string?
string[-1].
What is slicing?
Extracting a part of a string: string[start:end:step]
What does len(string) return?
The number of characters in the string.
How to concatenate strings?
With +
How to repeat a string?
With * (e.g., “Hi” * 3 → “HiHiHi”)
What does \n mean?
New line
What does \t mean?
Tab
What does \ mean?
A backslash
What is a raw string?
A string with r”” that treats backslashes literally (e.g., r”C:\new\test”)
What does .upper() do?
Converts string to uppercase
What does .replace(“old”,“new”) do?
Replaces substring with new text
What does .find(“sub”) do?
Returns index of first match, or -1 if not found
What are 3 main ways to format strings?
f-strings, .format(), % operator
Example of f-string?
f”My name is {name}”
Example of .format()?
“My name is {}”.format(name)
Example of % operator?
“My name is %s” % name
What is regex used for?
Pattern matching in strings
Common regex functions?
search, findall, split, sub
What does \d match?
Any digit
What does \D match?
Any non-digit
What does \w match?
Any word character (letters, digits, _)
What does \W match?
Any non-word character
What does \s match?
Any whitespace
What does \S match?
Any non-whitespace
What does \b match?
Word boundary
What does \B match?
Not at word boundary
What does re.split(r”\s”, text) do?
Splits text by whitespace
What does re.sub(pattern, replacement, text do?
) | Replaces matches with replacement