Knight Rider
Knight Rider

Reputation: 3

Removing the space before and after the number

This is the code

for x in range(1, 30):
    print"www.interpol.com/file/",x,'/en'

It prints this

www.interpol.com/file/ 1 /en
www.interpol.com/file/ 2 /en
www.interpol.com/file/ 3 /en

but i want to remove the spaces and want the result like

www.interpol.com/file/1/en
www.interpol.com/file/2/en
www.interpol.com/file/3/en

I think we can use /b or special characters like '

And if the want the result like this It worked, thanks. But I have one more question. Suppose I want the result to be like

www.interpol.com/file/30/en
www.interpol.com/file/60/en
www.interpol.com/file/90/en
www.interpol.com/file/120/en

then how to do that? This code worked :

> for x in range(1, 30):
>     print("www.interpol.com/file/{}/en".format(x))

Upvotes: 0

Views: 85

Answers (3)

ode2k
ode2k

Reputation: 2723

In Python3:

The easiest way to do this is to use the sep='' flag:

for x in range(1, 30):
    print("www.interpol.com/file/",x,'/en', sep='')

In Python2, you need to import the new print function first:

from __future__ import print_function

which customizes the separator between items in a print statement.

Upvotes: 0

Idos
Idos

Reputation: 15310

You can use + and cast x to str (I'm assuming this is Python):

>>> for x in range(1, 30):
        print("www.interpol.com/file/" + str(x) + '/en')


'www.interpol.com/file/1/en'
'www.interpol.com/file/2/en'

...

Upvotes: 1

Alec
Alec

Reputation: 653

Use the string's format method:

for x in range(1, 30):
    print("www.interpol.com/file/{}/en".format(x))

Upvotes: 1

Related Questions