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,457
  • 2
  • 19
  • 27

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
  • 462,703
  • 120
  • 1,088
  • 1,195
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,770
  • 18
  • 35
  • 54