0

I am trying to add suffix to an existing array. Below is my code

print('a' + [10, 100])

With this I am getting below error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str

Could you please help hoe to do that? I could use some for loop but I believe there may be more straightforward way to achieve the same.

3
  • The error message is clear. So you want a string as this - a10? Commented Jan 16, 2022 at 15:26
  • I want to get a new string array as [a10, a100] Commented Jan 16, 2022 at 15:27
  • Try this - ['a'+str(x) for x in [10, 100]] Commented Jan 16, 2022 at 15:30

2 Answers 2

2

You can create a new concatenated array as:

>>> ['{0}{1}'.format('a', num) for num in [10, 100]]
['a10', 'a100']

Read String format and List Comprehensions from doc.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand your question, you want a new string array (list). You could try this:

new_lst = ['a'+str(x) for x in [10, 100]] # just use string concatentation

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.