0

I have a function that returns me a list of strings. I need the strings to be concatenated and returned in form of a single string.

List of strings:

data_hold = ['ye la AAA TAM tat TE
0042

on the mountain sta
nding mute Saw hi
m ply t VIC 3181', 
'Page 2 of 3

ACCOUNT SUMMARY NEED TO GET IN TOUCH? ',
'YOUR USAGE BREAKDOWN

Average cost per day $1.57 kWh Tonnes']

I tried concatenating them as follows -

data_hold[0] + '\n' + data_hold[1]

Actual result:

"ye la AAA TAM tat TE\n0042\n\non the mountain sta\nnding mute Saw hi\nm ply t VIC 3181ACCOUNT SUMMARY NEED TO GET IN TOUCH? ',\n'YOUR USAGE BREAKDOWNAverage cost per day $1.57 kWh Tonnes'\n

Expected result:

'ye la AAA TAM tat TE
0042

on the mountain sta
nding mute Saw hi
m ply t VIC 3181', 
'Page 2 of 3

ACCOUNT SUMMARY NEED TO GET IN TOUCH? ',
'YOUR USAGE BREAKDOWN

Average cost per day $1.57 kWh Tonnes'
4
  • How did you print your actual result? It could be just a problem with wrong representation of the string. Commented Jun 21, 2019 at 1:57
  • You mean you don't want the \n in your result? Commented Jun 21, 2019 at 1:59
  • 1
    It seems to me that you want '\n\n'.join(data_hold). Commented Jun 21, 2019 at 2:01
  • I need final data in a single string, with all i in data_hold, so that I can then use regex to look for specific details. Commented Jun 21, 2019 at 2:17

2 Answers 2

1

Your 'expected result' is not a single string. However, running print('\n'.join(data_hold)) will produce the equivalent single string.

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

Comments

0

You misunderstand the difference between what the actual value of a string is, what is printed if you print() the string and how Python may represent the string to show you its value on screen.

For example, take a string with the value:

One line.
Another line, with a word in 'quotes'.

So, the string contains a single text, with two lines and some part of the string has the same quotes in it that you would use to mark the beginning and end of the string.

In code, there's various ways you can construct this string:

one_way = '''One line
Another line, with a word in 'quotes'.'''

another_way = 'One line\nAnother line, with a word in \'quotes\'.'

When you run this, you'll find that one_way and another_way contain the exact same string that, when printed, looks just like the example text above.

Python, when you ask it to show you the representation in code, will actually show you the string like it is specified in the code for another_way, except that it prefers to show it using double quotes to avoid having to escape the single quotes:

>>> one_way = '''One line
... Another line, with a word in 'quotes'.'''
>>> one_way
"One line\nAnother line, with a word in 'quotes'."

Compare:

>>> this = '''Some text
... continued here'''
>>> this
'Some text\ncontinued here'

Note how Python decides to use single quotes if there are no single quotes in the string itself. And if both types of quotes are in there, it'll escape like the example code above:

>>> more = '''Some 'text'
... continued "here"'''
>>> more
'Some \'text\'\ncontinued "here"'

But when printed, you get what you'd expect:

>>> print(more)
Some 'text'
continued "here"

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.