简体   繁体   中英

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

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'

...

Use the string's format method:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM