Hey just out of curiosity, the first example states ‘Alpha’, “Bravo” , & str(3); what is the str(3) purpose? When playing around with the function i noticed that i cannot set 3 = x. What does a str(3) do for strings?
str() is a function that returns the value as a
string. Read: Str() Python Docs.
In the example in the Codecademy lesson:
str(3), basically that means that
3 (3 is an
integer) will be
returned as a
string. Not too much to explain about it.
this_number = 3 #this is an integer this_string = "3" #this is a string because it's enclosed with ""s #you can try 'print'ing these and see the outcome
I know they both look the same when printed, but their data-type is different . If you dont remember much about data-types, you can do a google search about it, or review it in your lessons if you’ve learned about that already.
There are naming rules when making a variable. A plain number by itself cannot be used as a variable name.
List of naming rules quoted from Pythonkey* (same rules you'd find anywhere)
The name of the variable can only begin with an underscore or a letter (a-z, A-Z) which can be followed by letters, numbers (0-9) and underscores. For example: user_name, count, __name, pw123, etc.
All the python identifiers including variable names are case-sensitive. That is, userName and username are two different variables.
Variable names can not contain punctuation characters like &, %, *, etc.
Python keywords or reserved words can not be used as variable names. Here is a list of all the python keywords.
I hope that helps ! happy coding!
Alright cool, thank you. So calling str(3) would basically just allow for the integer 3 to be used in a string text?
Basically yes. Also, if you’re curious, you can just experiment in the editor and see what your results are when
Actually I have been. And because of which, I didnt understand the purpose of a str(3) call when if an integer can be called w/o str() when in quotes “”.
So you understand now?
I think it was part of the example for the lesson.
my_integer = 5 #just an integer my_string = str(my_integer) #stored a str version of my_integer
str() can be useful in different situations
Cool, thanks for your time