I Have a Question About Strings


#1

https://www.codecademy.com/courses/learn-python/lessons/strings--console-output/exercises/explicit-string-conversion?action=resume_content_item

Hello,

In the previously listed lesson I was asked to write str( ) before 3.14 in order to correct the sentence and turn it into a string, so it can be printed.

My question is why did I have to do that when I could have easily just put quotation marks around the number and it would’ve been successfully turned into a string, therefore it would’ve been printed?

Am I missing something here?

Is this just an example of another way to turn sentences into strings, if so in what ways would I have no other choice but to use it?


Python Noob Alert
#2

what if the number was user input? Or from a database? Or fetched from a website (data scraping)? This lesson teaches a concept that is important


#3

Thank you for your reply.
I just started learning code today, so I must admit I didn’t really understand what you just said.
Is there any way you could be more specific?


#4

well, currently you hard-coded the value (3.14) into your program, so yes, you can put quotation marks, but if the value comes from a database and is stored in a variable, you can’t put quotation marks around the value, so you will have to use str()


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.