Python introductory question: comma and str in print

Asked 2 years ago, Updated 2 years ago, 22 views

I'm solving the Python 300 for beginners

ice_cream = {'Merona': 1000, 'Papico' = 1100}

To output melona price: 1000 in

I wrote print('Melona Price:'(ice_cream['Melona'])) but they said str didn't work.

print('Melona Price:',(ice_cream['Melona'])) is the answer, but I don't know what str has to do with str instead of spacing one.

I'm sorry to ask you something basic because I'm a beginner. I'd appreciate it if you could explain it in detail

python

2022-09-21 12:04

1 Answers

In Python, a combination of strings and numbers is often used in one of three ways.

Many people are confused that print(1000) works as intended.
The reason why print('count: ' + 1000) is error is because 'count: ' and 1000 cannot be added.

Below is an example of how the code you uploaded was modified to work correctly.

ice_cream = { 
    Melona: 1000,
    Papico: 1100
}

# 1. Output using casting
print('Melona Price:' + str(ice_cream['Melona'])))
# 2. Output using commas
print('Melona Price:', ice_cream['Melona'])


2022-09-21 12:04

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.