Python Print Int as String

python int to string format
python string format
python concatenate string and int
concatenate integers python
print integer python 3
python float to string
python print integer format
python print without newline

So I'm kinda new to the python language and I'm messing around with various small problems to get used to the language.

Its pretty basic; two arrays of ints and a recursion that adds each level together.

When I go to print the values of each index, I get this error:

print(total[i]) TypeError: 'int' object is not subscriptable

Not quite sure that this means? I read somewhere you need to convert it to a string, not quite sure how to do that though.

If someone could point me in the right direction, that would be great.

total = 0

list1 = [207665, 340012, 860174, 830189, 39260, 502042, 582541, 595187, 748907, 627490, 871465]
list2 = [719571, 882606, 559276, 201999, 10167, 249982, 820978, 887870, 205461, 993892, 564934]

x = 11

for i in range(x):
    total[i] = list1[i] + list2[i]

for i in range(x):
    print(total[i])

Your problem is that you initialize total as an integer by assigning it the value 0 in the beginning. Afterward, you always assign it a new value in the first for loop but you always overwrite the existing value. The error is then thrown because you try to iterate through total which is not a list but an integer. Correct solution:

total = []

list1 = [207665, 340012, 860174, 830189, 39260, 502042, 582541, 595187, 748907, 627490, 871465]
list2 = [719571, 882606, 559276, 201999, 10167, 249982, 820978, 887870, 205461, 993892, 564934]

x = 11

for i in range(x):
    total.append(list1[i] + list2[i])

for i in range(x):
    print(total[i])

Concatenating string and integer in python, The easiest way is to convert int to a string using str() function. print(s + str(y)). Output: Year is 2018  The print function in Python The print function in Python is used to display the output of variables: string, lists, tuples, range etc. Before version 3.x of Python, the print was taken as a statement. However, from Python 3.x, the print acts as a function.


Make use of numpy for performance and elegance

import numpy as np
list1 = np.array([207665, 340012, 860174, 830189, 39260, 502042, 582541, 595187, 748907, 627490, 871465])
list2 = np.array([719571, 882606, 559276, 201999, 10167, 249982, 820978, 887870, 205461, 993892, 564934])
print(np.add(list1,list2))

Python Concatenate String and int, Printing string and integer (or float) in the same line. If a define a If you're using Python 3.6 you can make use of f strings. Which is pretty neat:  Like mentioned in the comments, it looks like you have re-assigned the built in function str to something else. Find and remove that, and your code should work. It's also worth noting that in python3 the print() function will attempt to cast your arguments to strings if you separate them with a ,.


You need use total as a list not as an int variable. A variable doesn't have any index, but a list supports indexing and storing multiple values. Like:

total = [] #updated

list1 = [207665, 340012, 860174, 830189, 39260, 502042, 582541, 595187, 748907, 627490, 871465]
list2 = [719571, 882606, 559276, 201999, 10167, 249982, 820978, 887870, 205461, 993892, 564934]

x = 11

for i in range(x):
    total.append(list1[i] + list2[i]) #updated

for i in range(x):
    print(total[i])

Printing string and integer (or float) in the same line, How do I print a string and int concatenate in Python? Python defines the type conversion functions to directly convert one data type to another data type, which is useful in day to day competitive programming. The int () function is a Python standard built-in function to convert the string into an integer value.


In your code total is int.So you could not use total[i](only list and tuple can)

And then use map() is better,or you can write like this

[list1[pos] + list2[pos] for pos in range(x)].

list1 = [207665, 340012, 860174, 830189, 39260, 502042, 582541, 595187, 748907, 627490, 871465]
list2 = [719571, 882606, 559276, 201999, 10167, 249982, 820978, 887870, 205461, 993892, 564934]

total = map(lambda x,y:x+y,list1,list2)
for i in total:
    print(i)

Java Convert String to int, Python String to Int: How to Convert a String to an Integer in Python age = 18 print("Hello, I am " + str(age) + " years old") # Output # Hello,  In Python, you can convert a Python int to a string using str(): >>> str ( 10 ) '10' >>> type ( str ( 10 )) <class 'str'> By default, str() behaves like int() in that it results in a decimal representation:


Python Variables and Assignment, Similar to the built-in str() method, Python also offers the handy int() method Converting a string to an integer int_age = int(age) print(int_age). The second and more usable way of formatting strings in Python is the str.format function which is part of the string class. The general syntax for using the str.format function is: format (value [, format_spec]) In this way of string formatting, we use placeholders in the string object. The placeholder is in the form of curly braces i.e. {}.


Python String to Int: How to Convert a String to an Integer in Python, You can convert a Python integer to a string using the built-in str function, or unicode on Python 2.7 if you need a unicode string. Convert a  To print strings and numbers in Python, is there any other way than doing something like: first = 10 second = 20 print "First number is %(first)d and second number is %(second)d" % {"first": first, " Stack Overflow


How to Convert Strings into Integers in Python, Another term for it is "string interpolation", because it interpolates various class types (like int, float and so on) into a formatted string. In many cases the string  What is the most Pythonic way to pad a numeric string with zeroes to the left, i.e. so the numeric string has a specific length? String formatting documentation. Unknown format code 'd' for object of type 'float'. – Cees Timmerman Jun 24 '14 at 13:32. Comments python >= 2.6 are incorrect. That syntax doesn't work on python >= 3.