Does str( ) change the value stored in the variable?

Using conversion functions like str() and float() does not change the value stored in a variable unless you assign the variable the value of itself converted, which is totally possible! Take a look at the code below for an example of this idea:

my_var = 10.0  
print “This is my_var’s value: “ + str(my_var)
my_var = int(my_var)
print “Now my_var is an int! Here it is: “ + str(my_var)
my_var = str(my_var) 
print “Now I can print it as normal because it’s a string: “ + my_var

Our variable starts off as a float, then we print it using str() to temporarily convert it so it can be concatenated with a string. Then it is actually assigned to the int version of itself. Finally, it becomes a string and we don’t need to convert it because you can concatenate strings together with no issue.

13 Likes

No it does not. All it does is changes the numeric value of a variable to a string.

1 Like

No because it just representation of value as you see in integer type you get only 10 the value of .0 is skip but in string just show the original value but its size does not change

1 Like

I was tinkering with this for a while, I used to code it like this :

float_1 = 0.25
float_2 = 40.0
product = int(float(float_1) * float(float_2))
big_string = "The product was " + str(product)

By Your advice my code would look like this :

float_1 = 0.25
float_2 = 40.0
product = float(float_1) * float(float_2)
product = int(product)
big_string = "The product was " + str(product)

I think it is much better to do it like yours, so when tracking back we could see every sequences in the code. But it all depends on the person working on it. Thanks

8 Likes

Hi,

I did it this way:

float_1 = 0.25
float_2 = 40.0
product = float(float_1 * float_2)
big_string ="The product was " + str(product)

1 Like

the interesting part is that the solution doesn’t have that change. even though my “run” wasn’t accepted because it was 10.0 not 10- CA solution wasn’t to add “int(” to the float…it only skipped line 3 making it empty.

I have a doubt regarding use of triple quotes. In your above example. I just wanted to print 2nd line. and for rest i put them in triple quotes. So as per your previous lessons, what ever in triple quote or started with# ( for each line) it should not be executed.
still I am getting syntax error.
where is the fault

The problem is that you pasted in the text from a different editor, one that substitutes ASCII double quotes with right- and left- quotes, which are not found in the ASCII character set.

capture

Python 2 won’t accept any non-ASCII characters.

Note that Python does not treat text enclosed in triple quotes as comments, i.e., it does not “ignore” them, as it does comments highlighted with the # character.

It treats triple-quoted strings as strings, which it will deal with and print while preserving the newlines. That is, you can use the triple-quotes to enclose multiline strings.

There is only one circumstance in which you should enclose something similar to a “comment” in triple-quotes, and that is immediately following the declaration of a function. This is called a docstring, and is used to tell you and your user what data types the function expects, what data types it will return, and any information about the function that the user needs to know. The docstring must be appropriately indented, and once your function is loaded into memory (i.e., “is running”), calling help on the function will return the docstring.

def area_cir(dia):
    """ input: int or float, the diameter of a circle
        output: float, the area of the circle.
    """
    return 3.14 * (dia/2)**2

>>> %Run test.py
>>> help(area_cir)
Help on function area_cir in module __main__:

area_cir(dia)
    input: int or float, the diameter of a circle
    output: float, the area of the circle.

(EDIT: “only one circumstance,” above, is a bit strong: triple quotes are frequently used at the top of a module or script to provide information concerning its function. The general idea, however, is (IMHO) correct: comments should be delineated by #. )

2 Likes

Something of note, this term only applies when it is immediately following the signature. Anywhere else in the function it is simply a string.

1 Like

I thought I had made that point; if not, thanks for the clarification.

1 Like

Thnks a ton for the clarification!

At the top of the file, one can choose a different encoding:

# coding: utf-8
print 'âââââ'

vim, probably also emacs, are also aware of that format or something compatible with it, and perhaps some other editors too.

python3 reads source files as utf-8 by default… one headache less.

@code5911381303 “commenting” out with multiline strings is totally fine, the strings get discarded since they’re not used. (But most programming text editors can help you out with commenting and de-commenting, and it would be more clear that it is a comment)
You’d have the same problem with a “real” comment, because it would still be unreadable characters.

Instructions

1.

Create a variable called product that contains the result of multiplying the float value of float_1 and float_2 .

print('Hello world!')

Checkpoint 2 Passed

2.

Create a string called big_string that says:

The product was X

with the value of product where the X is.

#The result below
float_1 = 0.25

float_2 = 40.0

product = float_1 * float_2

big_string = "The product was " + str(product)

#my problem is that am confused with the question,

#Questions

#1. i noticed that above code which is correct seems to have no X but rather space before string which i thought would result to syntex erro because i know opening and closing with syntex should match,

#2. Explain below question in lay mans language

Create a string called big_string that says:

The product was X

with the value of product where the X is.

i know X to be unknown in maths

was the product made X

big_string = “The product was X” + product

big_string = "The product was " + str(product)