python3 remove space from print

前端 未结 3 1190
渐次进展
渐次进展 2021-01-26 17:40

I\'ve got some simple python loop through a name to create a list of devices:

for i in range(18):
print(\"sfo-router\",(i))

The problem is it p

相关标签:
3条回答
  • 2021-01-26 17:49

    Change the sep parameter so that print doesn't implicitly insert a space:

    for i in range(18):
        print("sfo-router",(i), sep='')
    

    Alternatively, you can convert your number to a string with str and concatenate:

    for i in range(18):
        print("sfo-router" + str(i))
    

    Outputs: (in both cases)

    sfo-router0
    sfo-router1
    sfo-router2
    sfo-router3
    sfo-router4
    sfo-router5
    ...
    
    0 讨论(0)
  • 2021-01-26 17:55

    I'm new at python as well, and a quick google search did this one:

    Just use str.replace():

    string = 'hey man'
    string.replace(" ","")
    # String is now 'heyman'
    

    Source: Python remove all whitespace in a string

    0 讨论(0)
  • 2021-01-26 18:02

    Use format:

    for i in range(18):
        print("sfo-router{}".format(i))
    
    0 讨论(0)
提交回复
热议问题