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
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