0
0
Pythonprogramming~15 mins

Why strings are used in Python - See It in Action

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use an else block to print the not found message. Use f-strings to insert variables in the print.