How can I read a text file into a string variable and strip newlines?

How can I read the file into a single-line string without newlines, in this case creating a string 'ABCDEF' ? For reading the file into a list of lines, but removing the trailing newline character from each line, see How to read a file without newlines?.

31.5k 22 22 gold badges 109 109 silver badges 132 132 bronze badges asked Dec 3, 2011 at 16:47 16.2k 8 8 gold badges 35 35 silver badges 52 52 bronze badges Commented Oct 31, 2016 at 22:22 The title and the question are inconsistent. Do you really want to get rid of the \n as well? Commented Jul 18, 2017 at 10:11

do you really want to remove newlines from the file/string contents, or are you just confused about the many meta-characters in your print output and actually want to keep the newlines, but not have them display as "\n"?

Commented Dec 5, 2017 at 18:25

Do you really want to read the entire text into one string variable? Do you really mean with "strip newlines" to replace them with an empty string? This would mean, that the last word of a line and the first word of the next line are joined and not separated. I don't know your use case, but this seems to be a strange requirement. I might have another answer if you explain what you intend to do with the read in data

Commented Jan 7, 2021 at 0:47

@gelonida based on the answers that were given, the original question text, and which answer was accepted, that is exactly what OP wanted. The first version of the question read: "As i see data is in list form. How do i make it string. And also how do i remove \n, [, and ] characters from it ?" - suggesting severe misunderstanding of the fundamentals, but pretty adamant and clear about the desired output.