FAQ: Learn Python: Syntax - Variables

This community-built FAQ covers the “Variables” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Variables

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Can print be used on more than just strings?

2 posts were split to a new topic: Why change a variable rather than just create a new one?

In practice exercise of variables , outputs are somewhat like
breakfast:
A muffin

But i want to get the output like
Breakfast: A muffin (in same line and using variable syntax).Please help me out

I am totally new in programming field.

1 Like

I am getting error while running this code. What’s the issue?

line1 = “I love you”
line2 = “I miss you”
print(line1\n\line2)

I tried mixing variable and line break.

Keep in mind that \n is an escape character and must go between quotes, think of it as a word. You can try something like:

print(line1 + "\n" + line2)
1 Like

How to get the output like
Breakfast: A muffin.
In same line.

I formulated this, still has error.

We’ve defined the variable “meal” here to the name of the food we ate for breakfast!

meal = “An english muffin”

Printing out breakfast

print(“Breakfast:”)

print(meal)

Now update meal to be lunch!

meal = “A big sandwich”

Printing out lunch

print(“Lunch:”)

print(meal)

Now update “meal” to be dinner

meal = “lasagna”

Printing out dinner

print(“Dinner:”)

print(meal)

I have a few questions concerning Variables:

  • First of all, will a string always be a variable as long as I use the format (where “string” can be anything that doesn’t start with a number and only has underscores “_” or numbers within it)

string = “example”

  • Second, if I am using the same variable name multiple times, will the variables associated data (block of texts or anything else) always be the one associated to the latest one?

For example, for the following code:

meal = “omelette”
print(“Breakfast:”)
print(meal)
print(“Brunch:”)
print(meal)

print(meal) would always print the latest precision for meal, right? (In this case, omelette)

If I were to include another designation for meal, the latest one would override the initial one?

In this case:

meal = “omelette”
print(“Breakfast:”)
print(meal)
meal = “hashbrown”
print(“Brunch:”)
print(meal)

According to what I understand, print(meal) would display “omelette” first instance, and then display “hashbrown” for the second instance. Am I correct on this point?

Thus, if I want to have the initial designation, I would have to write another line of

meal = “omelette”

So that for subsequent print(meal), I should be able to only print “omelette” instead of “hashbrown”?

Thank you in advance for your answers!