Data Types And String Manipulation Flashcards
What is a data type?
A classification that tells the computer what kind of data is being used and how it should be stored and processed.
What are the main data types in Python?
• Integer (int): Whole numbers (e.g., 5, -10)
• Float: Decimal numbers (e.g., 3.14, -0.01)
• String (str): Text enclosed in quotes (e.g., “hello”)
• Boolean (bool): True or False
What is a string in Python?
A sequence of characters enclosed in quotation marks (e.g., “Computer”).
How can you access characters in a string in Python?
Using indexing: string[index], where indexing starts at 0.
For example, “hello”[1] returns “e”.
How do you get the length of a string in Python?
Use the len() function — e.g., len(“hello”) returns 5.
How do you slice a string in Python?
Using string[start:end], which returns characters from the start index up to but not including the end index.
Example: “hello”[1:4] returns “ell”.
What does string.upper() do in Python?
Converts all letters in the string to uppercase.
What does string.lower() do in Python?
Converts all letters in the string to lowercase.
What does string.strip() do in Python?
Removes whitespace (or other characters) from the beginning and end of the string.
What does string.replace(old, new) do in Python?
Replaces all instances of the old substring with new.
Example: “hello”.replace(“l”, “x”) → “hexxo”.
What does string.find(sub) do in Python?
Returns the index of the first occurrence of the substring sub. If not found, it returns -1.
How do you concatenate (join) strings in Python?
Use the + operator.
Example: “Hello “ + “World” → “Hello World”.
How do you convert a string to an integer in Python?
Use int(string).
For example: int(“5”) → 5.
How do you convert an integer to a string in Python?
Use str(integer).
For example: str(10) → “10”.
What is string iteration in Python?
Using a loop to go through each character in a string, e.g.:
for letter in “hello”:
print(letter)
Why is it important to understand string manipulation in programming?
Because strings are used frequently in input/output, data processing, and communication with.