Python Forum
Help me understand variable value
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me understand variable value
#1
Hello!

I recently stumbled upon a python course and a test of sorts, there is a question:

What is the value of C?

T = 3
v = "ab"
C = T *v

To which I replied that the value is ababab

I was corrected and told that the value is 'ababab'

Can you help me understand why this is the case? Thank you
Reply
#2
The value of C is a str object containing ababab. The value of all python variables are objects. Even numbers are int or float objects. In C = 3 + 4, the value of C is the int object 7

'ababab' is a string literal, code you include in a program to create a str object. It is also a convention for representing a str object, but it is not the "value" of C. str objects don't have starting and ending quotes, so saying the "value" of a str object has quotes is only correct because of a convention used for representing strings that way. It is like saying [1, 2, 3] is a list. [1, 2, 3] can be a list literal, code that you write in your program to create a list of int. [1, 2, 3] is also a convention used when describing the contents of value of a list. But lists don't have square brackets just as strings don't have quotes (though a string can contain quotes).

If this was a question you were asked on a test, I think it was a dumb question.
buran likes this post
Reply
#3
(Feb-15-2025, 02:48 PM)deanhystad Wrote: If this was a question you were asked on a test, I think it was a dumb question.
i would guess it was also computer-evaluated exam/test
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Yes! The difference between ababab and 'ababab' is just the way the output is represented.

### Let's break it down:

#### The given code:
`python
T = 3
v = "ab"
C = T * v
`
- T = 3 → This is an integer.
- v = "ab" → This is a string.
- C = T * v → This means "ab" is repeated 3 times, which results in "ababab".

#### Why the correction?
Your answer (ababab) is correct in content but lacks quotation marks, which Python uses to indicate a string. In Python:
- If you print C, it will output **ababab** (without quotes), because print() displays the string's content.
- But the actual value of C is **'ababab'** (or "ababab"), because strings in Python are always enclosed in quotes when represented formally.

#### Correct Answer:
If they are asking for the exact value, it should be **'ababab'** or **"ababab"** (both are valid representations of strings in Python). Your answer was conceptually correct but might have been expected in Python's string notation.

Let me know if that clears it up! 😊
Reply
#5
(Feb-19-2025, 03:57 AM)Sece1967 Wrote: Yes! The difference between ababab and 'ababab' is just the way the output is represented.

### Let's break it down:

#### The given code:
`python
T = 3
v = "ab"
C = T * v
`
- T = 3 → This is an integer.
- v = "ab" → This is a string.
- C = T * v → This means "ab" is repeated 3 times, which results in "ababab".

#### Why the correction?
Your answer (ababab) is correct in content but lacks quotation marks, which Python uses to indicate a string. In Python:
- If you print C, it will output **ababab** (without quotes), because print() displays the string's content.
- But the actual value of C is **'ababab'** (or "ababab"), because strings in Python are always enclosed in quotes when represented formally.

#### Correct Answer:
If they are asking for the exact value, it should be **'ababab'** or **"ababab"** (both are valid representations of strings in Python). Your answer was conceptually correct but might have been expected in Python's string notation.

Let me know if that clears it up! 😊

Can you schedule an appointment at the hairdresser for me?
Gribouillis likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020