0

I have a loop script that makes a list of strings from a file using the line.split() function, although I want to tag each string with two other strings that form during the loop.

How can I concatenate each string formed from the line.split() function to two other strings?

This is the relevant part of my code

out = str()

for fileName in os.listdir(os.getcwd()):

if line.startswith('Group:'):
    y = line.split()
    t_group = y[]
    group = t_group

if line.startswith("F:"):
   y = line.split()
   #this is where the list of strings is formed

   t_out = group + "    " + ___ # The underscores are a placeholder for each string in the list that I want to add
   out.append(t_out)
4
  • where has been defined line? Commented Jan 7, 2020 at 17:22
  • hey, Can you show the expected output? Commented Jan 7, 2020 at 17:28
  • Yes so it should be group + \t + single string from the line.split() Commented Jan 7, 2020 at 17:45
  • and each new string (consisting of one of the strings from the line.split() and the group) would be appended to out. Commented Jan 7, 2020 at 17:46

1 Answer 1

1

If you just want to concatenate all strings in a list, use join:

strs = ["hey", "what's", "up"]
concat = "".join(strs)
print(concat)

# output
heywhat'sup

To append one string at a time, you could do something like:

output_string = "prepended text"
for string in strs:
    output_string += string
    print(output_string)

printed output would be:

prepended text
prepended texthey
prepended textheywhat's
prepended textheywhat'sup

Of course, you could add a space into the string each time you append your string from the list too.

output_string += f" {string}"

which would print:

prepended text
prepended text hey
prepended text hey what's
prepended text hey what's up
Sign up to request clarification or add additional context in comments.

1 Comment

I do not want to use all of the strings at once though. I want the script to use each string, one at a time, to be concatenated to the group string and then to be appended to out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.