Python add number to string

Try

 Url = (Url) + str(count)

instead. The problem was that you were trying to concatenate a string and a number, rather than two strings. str() will fix this for you.

str() will provide a string version of count suitable for concatenation, without actually converting count to a string from an int. See this example:

>>> n = 55

>>> str(n)
>>> '55'

>>> n
>>> 55

Lastly, it is considered more efficient to format a string, rather than concatenate it. I.e.,

 Url = '%s%d' % (Url, count)

or

 Url = '{}{}'.format(Url, count)

Also, you have an infinite loop since the value of count is never changed inside the loop. To fix this add

count += 1

at the bottom of your loop.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while dealing with strings, we may encounter a problem in which we might have a numeric variable whose value keeps changing and we need to print the string including that number. Strings and numbers being different data types have to be solved in different ways. Let’s discuss certain ways in which this problem can be solved. 

    Method #1 : Using Type conversion The simplest way in which this task can be performed is by converting the integer explicitly into string datatype using the basic type conversion and adding it to appropriate position. 

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res = test_str + str(test_int) + test_str

    print("The string after adding number is  : " + str(res))

    Output : 

    The original string is : Geeks
    The original number : 4
    The string after adding number is  : Geeks4Geeks

    Method #2: Using %d operator This operator can be used to format the string to add the integer. The “d” represents that the datatype to be inserted to string is an integer. This can be changed according to the requirements. 

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res = (test_str + "% d" + test_str) % test_int

    print("The string after adding number is  : " + str(res))

    Output : 

    The original string is : Geeks
    The original number : 4
    The string after adding number is  : Geeks4Geeks

    Method #3: Using join() method

    Python3

    test_str = "Geeks"

    test_int = 4

    print("The original string is : " + test_str)

    print("The original number : " + str(test_int))

    res=[test_str]*2

    res=str(test_int).join(res)

    print("The string after adding number is : " + str(res))

    Output

    The original string is : Geeks
    The original number : 4
    The string after adding number is : Geeks4Geeks


    Python supports string concatenation using + operator. In most of the programming languages, if we concatenate a string with an integer or any other primitive data types, the language takes care of converting them to string and then concatenate it. However, in Python, if you try to concatenate string and int using + operator, you will get a runtime error.

    Python Concatenate String and int

    Let’s look at a simple example to concatenate string and int using + operator.

    s = 'Year is '
    
    y = 2018
    
    print(s + y)
    

    Output:

    Traceback (most recent call last):
      File "/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in 
        print(s + y)
    TypeError: can only concatenate str (not "int") to str
    

    So how to concatenate string and int in Python? There are various other ways to perform this operation.

    Using str() function

    The easiest way is to convert int to a string using str() function.

    print(s + str(y))
    

    Output: Year is 2018

    Using % Operator

    print("%s%s" % (s, y))
    

    Using format() function

    We can use string format() function too for concatenation of string and int.

    print("{}{}".format(s, y))
    

    Using f-strings

    If you are using Python 3.6 or higher versions, you can use f-strings too.

    print(f'{s}{y}')
    

    You can checkout complete python script and more Python examples from our GitHub Repository.

    Want to learn more? Join the DigitalOcean Community!

    Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

    Sign up

    How do you add a number to a string?

    Note that all strings have to be converted to numbers before using the addition (+) operator..
    Pass each string to the Number object to convert it to a number..
    Use the addition (+) operator, e.g. Number('1') + Number('2') ..
    The addition operator will return the sum of the numbers..

    How do you add a value to a string in Python?

    The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded. Two strings are added using the + operator.

    How do you add numbers in a string in Python list?

    Just convert your individual string digits to integers before turning them into a list: numbers = [int(digit) for digit in str(number)] addition = sum(numbers) average = addition / len(numbers) # etc.

    How do you add numbers in Python?

    How to Add Two Numbers in Python.
    ❮ Previous Next ❯.
    Example. x = 5. y = 10. print(x + y) Try it Yourself ».
    Example. x = input("Type a number: ") y = input("Type another number: ") sum = int(x) + int(y) print("The sum is: ", sum) Try it Yourself ».