0

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:

enter image description here

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.

2
  • You can't get the string value of something that doesn't exist. dog doesn't exist until you set it to something. Aug 20, 2016 at 19:10
  • 1
    I'm not sure why this question has been downvoted to -3. It's a very basic question, but it's not a bad question. Aug 21, 2016 at 7:13

2 Answers 2

1

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"""
3
  • So, are you telling me that I'm working in a program that doesn't even have similar rules as a real Python and is just a substitute of it? Because, that would mean that I'm learning nonsense :D that has no real application later on... Aug 20, 2016 at 18:20
  • I don't follow. Learn about strings of the "real" python docs.python.org/2/library/string.html Aug 20, 2016 at 18:41
  • @Jakub No, that's not what Esben is saying. The pieces of code str(45161516) and str(True) are valid Python. Aug 21, 2016 at 7:02
2

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 expression dog 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.

1
  • 1
    "However, the expression dog does not have a value": You could expand on this and say that it is parsed and evaluated as a variable name, and not as a string literal, as the OP seems to imply in their question.
    – Giorgio
    Aug 21, 2016 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.