Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Strings Are Used
๐ Scenario: Imagine you are creating a simple contact list app. You need to store names and phone numbers. Names are words made of letters, so you use strings to keep them.
๐ฏ Goal: You will create a dictionary with names as strings and their phone numbers. Then you will check if a name is in the list and print a message.
๐ What You'll Learn
Create a dictionary called contacts with exact string keys and integer values
Create a variable called search_name with a string value
Use an if statement to check if search_name is in contacts
Print a message showing the phone number if found, or a not found message
๐ก Why This Matters
๐ Real World
Storing and searching names in contact lists, user databases, or any place where words identify data.
๐ผ Career
Understanding strings and dictionaries is essential for data handling in software development, customer management, and many programming tasks.
Progress0 / 4 steps
1
Create the contacts dictionary
Create a dictionary called contacts with these exact entries: 'Alice': 12345, 'Bob': 67890, 'Charlie': 54321
Python
Hint
Use curly braces {} to create a dictionary. Names must be in quotes because they are strings.
2
Set the search name
Create a variable called search_name and set it to the string 'Bob'
Python
Hint
Remember to put the name in quotes because it is a string.
3
Check if the name is in contacts
Use an if statement to check if search_name is in contacts
Python
Hint
Use if search_name in contacts: to check presence. Access the phone number with contacts[search_name].
4
Print the result
Print "Bob's phone number is 67890" if search_name is in contacts, otherwise print "Bob is not in contacts"
Python
Hint
Use an else block to print the not found message. Use f-strings to insert variables in the print.
Practice
(1/5)
1. Why do programmers use strings in Python?
easy
A. To store text like words and sentences
B. To perform mathematical calculations
C. To create loops and conditions
D. To store numbers only
Solution
Step 1: Understand what strings represent
Strings are used to hold text such as words and sentences, not numbers or calculations.
Step 2: Identify the correct use of strings
Since strings hold text, they help programs communicate with people using readable words.
Final Answer:
To store text like words and sentences -> Option A
Quick Check:
Strings = Text storage [OK]
Hint: Strings always hold text inside quotes [OK]
Common Mistakes:
Thinking strings are for math calculations
Confusing strings with numbers
Believing strings control program flow
2. Which of the following is the correct way to write a string in Python?
easy
A. text = 'Hello'
B. text = Hello
C. text = Hello'
D. text = Hello"
Solution
Step 1: Check string syntax rules
Strings must be enclosed in matching quotes, either single ('') or double ("").
Step 2: Identify the correct option
Only text = 'Hello' uses matching single quotes around Hello, making it a valid string.
Final Answer:
text = 'Hello' -> Option A
Quick Check:
Strings need quotes [OK]
Hint: Strings must be inside matching quotes [OK]
Common Mistakes:
Forgetting quotes around text
Using mismatched quotes
Using quotes only on one side
3. What will be the output of this code?
name = 'Alice'
print('Hello, ' + name + '!')
medium
A. Hello, name!
B. Hello, + name + !
C. Hello, Alice!
D. Error: cannot add string and variable
Solution
Step 1: Understand string concatenation
The + operator joins strings together. Here, 'Hello, ' + name + '!' combines the parts into one string.
Step 2: Substitute the variable value
Variable name holds 'Alice', so the output becomes 'Hello, Alice!'.
Strings must be inside quotes. Here, Hello is not quoted, so Python treats it as a variable.
Step 2: Identify the error cause
Since Hello is not defined as a variable, this causes a NameError.
Final Answer:
Missing quotes around Hello -> Option D
Quick Check:
Strings need quotes to avoid errors [OK]
Hint: Always put quotes around text strings [OK]
Common Mistakes:
Forgetting quotes around text
Assuming print is misspelled
Thinking variable names cause error
5. You want to create a program that asks a user for their name and then greets them with a message. Which of these code snippets correctly uses strings to do this?
hard
A. name = input(Enter your name: )
print('Hello, ' + name + '!')
B. name = input('Enter your name: ')
print('Hello, ' + name + '!')
C. name = input('Enter your name: ')
print('Hello, name!')
D. name = input('Enter your name: ')
print('Hello, ' + 'name' + '!')
Solution
Step 1: Check input function usage
Input prompt must be a string inside quotes. name = input('Enter your name: ')
print('Hello, ' + name + '!') uses 'Enter your name: ' correctly.
Step 2: Verify greeting message construction
name = input('Enter your name: ')
print('Hello, ' + name + '!') concatenates 'Hello, ' + name + '!' so the user's input is included in the greeting.
Final Answer:
name = input('Enter your name: ')
print('Hello, ' + name + '!') -> Option B
Quick Check:
Input prompt and string concat correct [OK]
Hint: Use quotes for prompts and + to join strings [OK]