简体   繁体   English

Python:为没有空格的变量分配字母和数字

[英]Python: Assign letter and number to a variable without spaces

I was wondering.我想知道。 how can I get the output like this, and store it in to a string?我怎样才能得到这样的输出,并将其存储到一个字符串中?

from __future__ import print_function

for i in range(0,3):
    if i > 0:
        print("G",i,sep="")

output:输出:

G1
G2

I was wondering how I can store the values into a string?我想知道如何将值存储到字符串中? (for-loop) (循环)

Following code can work:以下代码可以工作:

from __future__ import print_function
st = ""
for i in range(0,3):
    if i > 0:
        print("G",i,sep="")
        st += "G"+str(i)+"\n"
print (st)

which shall give you the following output:这将为您提供以下输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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