aNumber = 8 # set a number variable
aString = str(aNumber) # this way will work
#aString = aNumber # this way will fail
result = 'a string combined with the number: ' + aString
print(result)
# or use it directly in another statement
aNumber = 8
print('a string combined with the number: ' +
str(aNumber))
If you have any doubts, please give me comment...
str function will convert integer/float/list into string format.
we can't directly add integer to string, so that we need convert integer to string by using str function i.e., str(aNumber).
Explanation:
aNumber = 8 # set a number variable
aString = str(aNumber) # this way will work
#aString = aNumber # this way will fail
# the above statement will work but aString variable convert into integer. so for the next statement it raises raises an error
result = 'a string combined with the number: ' + aString
print(result)
Let me know if you have any clarifications. Thank you...
Get Answers For Free
Most questions answered within 1 hours.