0

I have make a set of names in this form: 's1', 's2', ..., 's100'. I thought I can do that easily via looping:

for i in range(100):
    print ('s'.format(i+1))

format here does not append the numbers. I only get ss..ss without the numbers being concatenated in single quote. I know how to do this in Java but I am not that much expert in Python. Thank you

Kristofer
  • 1,327
  • 2
  • 16
  • 26

2 Answers2

3

You need to have a placeholder in the format string:

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument.

for i in range(100):
    print ('s{0}'.format(i+1))
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
2

If you use 3.6, then you can take advantage of the new 'Literal String Interpolation', and do the following:

for i in range(100):
    print(f's{i + 1}')

For more details on this feature, check out PEP 498 -- Literal String Interpolation

Peter Pei Guo
  • 7,612
  • 17
  • 33
  • 53