Python Integer and String Using [duplicate]

2024/9/18 22:05:15
for size in [1, 2, 3, 4]:result = 0print("size=" + str(size))for element in range(size):result = result + elementprint(" adding " + str(element)+", result so far=" + str(result))print("Done. size=" + str(size) + " result=" + str(result))
print("All done!")

I wonder why we use the str() function on size and result in the 7th line. I know we can't use integer and string in a single print function, but if the rule is like this, why isn't there any problem in this code (about integer + string rule)?

x=18
print("Hello , I am" , x , "years old.")

We use integer and string in a single line don't we ?

Answer

It's about the order things are run:

"Done. size=" + str(size) + " result=" + str(result)

has to be done first, before the print function is called. The print function just gets the single joined string - it has no idea that a + was used to construct it.

In this case:

print("Hello , I am" , x , "years old.")

the print function gets all 4 arguments (actually, a 4 element tuple) and can now work its magic on converting each one to a string itself (see Unnecessary detail below).

By the way, if you object to having to use str() then there are other ways of formatting the string to print. The following lines all produce the same output:

print("Done. size=" + str(size) + " result=" + str(result))
print("Done. size=%d result=%d" % (size, result))
print("Done. size={} result={}".format(size, result))
print(f"Done. size={size:} result={result:}")

The last one f" " requires Python 3.6 or later.

Unnecessary detail:

The conversion to str() in the print() function is actually done (in the C implementation) by the python API call PyFile_WriteObject() using the flag Py_PRINT_RAW. If Py_PRINT_RAW is given, the str() of the object is written.

https://en.xdnf.cn/q/119765.html

Related Q&A

Beginner to python: Lists, Tuples, Dictionaries, Sets [duplicate]

This question already has an answer here:What is the difference between lists,tuples,sets and dictionaries? [closed](1 answer)Closed 3 years ago.I have been trying to understand what the difference is…

TypeError: NoneType object is not iterable in Python in csv

I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an er…

No such Element Exception using selenium in python

from selenium import webdriver from selenium.webdriver.common.keys import Keys chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe" driver=webdriver.Chro…

Web scraping, cant get the href of a tag

Im trying to scrape this Page https://rarity.tools/thecryptodads Using Selenium in python. At the top of the right of each card below, theres the owner name that contains a link once pressed, it takes …

Using Python Pandas to fill new table with NaN values

Ive imported data from a csv file which has columns NAME, ADDRESS, PHONE_NUMBER. Sometimes, at least 1 of the columns has a missing value for that row. e.g0 - Bill, Flat 2, 555123 1 - Katie, NaN, NaN 2…

sympy AttributeError: Pow object has no attribute sin

I have read this SO post which says namespace conflict is one reason for this error. I am falling to this error frequently. So, Id like to learn what exactly is happening here? What is expected by the…

Tkinter unbinding key event issue

In the code below, pressing the space bar twice results in two successive beeps. I want to avoid this and instead disable the key while the first beep is happening. I thought unbinding the space key mi…

Is there a way to find the largest change in a pandas dataframe column?

Im trying to find the largest difference between i and j in a series where i cannot be before j. Is there an efficient way to do this in pandas:x = [1, 2, 5, 4, 2, 4, 2, 1, 7] largest_change = 0for i i…

Updating scikit-learn to latest version with Anaconda environment fails with http error 000

I use Anaconda3 installed on my pc Win10 64bits. I noticed it runs with an outdated scikit learn version (0.21.3), and I am trying to update it (0.24.1 available on https://repo.anaconda.com/pkgs/main/…

Python RegEx remove new lines (that shouldnt be there)

I got some text extracted and wish to clean it up by RegEx.I have learned basic RegEx but not sure how to build this one:str = this is a line that has been cut. This is a line that should start on a …