String Printing Https://www.codecademy.com/courses/learn-python/lessons/strings--console-output/exercises/and-now-for-something-completely-familiar

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. :slight_smile:

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.

Example
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 :slight_smile: . 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)

Python naming rules*

  • 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 :slight_smile: ! happy coding!

1 Like

Alright cool, thank you. So calling str(3) would basically just allow for the integer 3 to be used in a string text?

2 Likes

Basically yes. Also, if you’re curious, you can just experiment in the editor and see what your results are when printing. It helps you understand things better :slight_smile: .

1 Like

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 “”.

2 Likes

So you understand now?


I think it was part of the example for the lesson. :slight_smile:


Extra:

my_integer = 5 #just an integer

my_string = str(my_integer)  #stored a str version of my_integer

so, str() can be useful in different situations :slight_smile:

2 Likes

Cool, thanks for your time

2 Likes