4

So this is my problem:

first I iterate

for i in range(1,21):
    print (i)

then I have a variable basename="Station"

I want a result where the output is Station_1.txt, Station_2.txt etc.

I managed to add the "_" to the variable basename like

mylist1 = ("_")
s = (basename)
for item in mylist1:
    s += item

but now I have the problem on how to get the iteration on top of the variable basename, I could get is as it own like "Station_, 1, Station_, 2" but not inside the string itself.

Sorry, a total beginner here :)

0

5 Answers 5

9

You can do this in one line! Try the following:

basename = "Station"
result = ["{}_{}.txt".format(basename, i) for i in range(1, 21)]
print(result)`

 >> ['Station_1.txt','Station_2.txt','Station_3.txt', 
     'Station_4.txt','Station_5.txt','Station_6.txt', 
     'Station_7.txt','Station_8.txt','Station_9.txt', 
     'Station_10.txt','Station_11.txt',Station_12.txt',
     'Station_13.txt','Station_14.txt','Station_15.txt',
     'Station_16.txt','Station_17.txt','Station_18.txt',
     'Station_19.txt','Station_20.txt']`
Sign up to request clarification or add additional context in comments.

Comments

3

Do you want something like this?

basename = "Station"
for i in range(1, 21):
    value = basename + "_" + str(i) + ".txt"
    print(value)

output:

Station_1.txt
Station_2.txt
Station_3.txt
Station_4.txt
Station_5.txt
Station_6.txt
Station_7.txt
Station_8.txt
Station_9.txt
Station_10.txt
Station_11.txt
Station_12.txt
Station_13.txt
Station_14.txt
Station_15.txt
Station_16.txt
Station_17.txt
Station_18.txt
Station_19.txt
Station_20.txt

Comments

2

I would use .format(). See https://docs.python.org/3.3/library/stdtypes.html#str.format

For example:

base_string = 'Station'
for i in range(1,21):
    print('{}_{}.txt'.format(base_string, i))

1 Comment

In python 3.6+ you can also use f string print(f"{base_string}_{i}') . Docs: docs.python.org/3/reference/lexical_analysis.html#f-strings
1

You can use string formatting to forget about worrying about integers

You still loop over with the number as your parameter and keep the base name the same:

base = 'Station'
for i in range(1, 21):
    name = '{}_{}.txt'.format(base, i)
    print(name)  # or do whatever with 'name'

Comments

1

String concatenation is best avoided if possible. An alternative is Python now supports format strings by prefixing f which could be used to solve your problem as follows:

base_string = 'Station'

for i in range(1, 21):
    print(f'{base_string}_{i}.txt')    

This was added in Python 3.6. It could also be useful to zero pad your strings which would ensure the strings are sorted correctly:

base_string = 'Station'

for i in range(1, 21):
    print(f'{base_string}_{i:02}.txt')

This would give you:

Station_01.txt
Station_02.txt
Station_03.txt
Station_04.txt
Station_05.txt
Station_06.txt
Station_07.txt
Station_08.txt
Station_09.txt
Station_10.txt
Station_11.txt
Station_12.txt
Station_13.txt
Station_14.txt
Station_15.txt
Station_16.txt
Station_17.txt
Station_18.txt
Station_19.txt
Station_20.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.