Variable can't hold str() method information with “pure” word in brackets… why?
https://softwareengineering.stackexchange.com/questions/328880
문제
I've recently bumped into string methods and mainly
str()
method caught my interest because, it makes string out of every type of data? integers, booleans and so on. And if you want, they can be easily stored in variables like new strings:
my_string = str(45161516)
that variable will automatically hold that number as a string and same with the boolean:
my_string = str(True)
but, when you put pure word "dog" for example:
my_string = str(dog)
variable won't hold it as a string... Here's also an image when I was trying out this concept during my Python course:
Can I ask why is it so?
Why the variable doesn't return value and why
str(dog)
doesn't make a string "dog"?
Is it because "dog" in no datatype so the method doesn't recognize it?
Or it's just way it is and don't poke the beehive. At first I thought that variables can't hold string methods but it turned out that
my_variable = str(pure word)
is the only case when variable doesn't hold and return anything.
해결책
Because it is not valid python. A string literal has to be defined in one of the following ways.
str1 = "test"
str2 = 'test2'
str3 = """test3"""
다른 팁
In Python, when you call a function with an argument, that argument must always be a valid expression. The pieces of code 45161516
and True
are valid expressions, but dog
is not a valid expression (if it has not been defined anywhere). So you cannot use dog
in a function argument if dog
has not been defined.
It probably looks like the str
method creates a string out of whatever text you type between the parentheses, but this is not actually how str
works at all.
Here is how Python evaluates str(45161516)
:
- First, Python evaluates the expression
str
to obtain a function. The function that it obtains is the "str" function. - Then, Python evaluates the expression
45161516
to obtain an argument. The argument that it obtains is the number 45161516. - Python calls the function (which is the "str" function) with the argument (which is the number 45161516) and obtains a result. The result is the character string "45161516".
And here's how Python evaluates str(True)
:
- First, Python evaluates the expression
str
to obtain a function. The function that it obtains is the "str" function. - Then, Python evaluates the expression
True
to obtain an argument. The argument that it obtains is the boolean value "true". - Python calls the function (which is the "str" function) with the argument (which is the boolean value "true") and obtains a result. The result is the character string "True".
Finally, here's how Python tries to evaluate str(dog)
:
- First, Python evaluates the expression
str
to obtain a function. The function that it obtains is the "str" function. - Then, Python attempts to evaluate the expression
dog
to obtain an argument. However, the expressiondog
does not have a value (if it has not been defined), so the evaluation fails.
Of course, you can define a variable called dog
if you want. You can run this code:
dog = 12345
print str(dog)
Since the value of the variable dog
is the number 12345, when Python evaluates the expression dog
to obtain an argument, the argument that obtains will be the number 12345. So the result of the "str" function will be the character string "12345", and this string will be printed.