Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python add string and int

# To combine an str and int you need to convert the int to a str first.
# You can do this all on one line however like so. Just keep adding plus signs between statements:
string = 'Hello there ' + str(your_variable1_here) + ' oh my more text ' + str(your_variable2_here)

# Working example that makes sure all items in a list are strings:
list = ['hello', 'howdy', 73, 'greetings']
for i, c in enumerate(list):
    assert isinstance(c, str), 'List index ' + str(i) + ' was invalid! ' + '(It's value is: ' + str(c) + ')'

# Will output this error because '73' is an INT object and not a STR object:
# AssertionError: List index 2 was not valid! (73)
 
PREVIOUS NEXT
Tagged: #python #add #string #int
ADD COMMENT
Topic
Name
6+4 =