Replacing newline doesn't work [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 7 months ago.
I have a .txt file form which I read multiple lines and append an array with every line.
Unfortunately I also have the line breaks in the array.
When I try to replace them with line.replace("\n", ""), nothing will happen.

Are you just doing line.replace("\n", "")? If so, that's the problem. You are doing the replacement, then throwing away the result. You need:
line = line.replace("\n", "")

I had the same problem, but even saving the result in another variable. I started breaking it in code units to find the problem and I found that my input had carriage returns, which is '\r'. They made visually the same result as the '\n' in the output file. So I fixed it by doing the following:
result = input.replace("\n", "").replace("\r", "");

Related

How do I stop spaces appearing in this program? [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
In class im meant to write a program that arranges coordinates for you. I wrote this:
x = input("")
y = input("")
z = input("")
print("(",x,",",y,",",z,")\n)
and the output is: (␣0␣,␣-7831␣,␣2323␣)⤶
how do I stop the extra spaces from appearing so I get this?: (0,␣-7831,␣2323)⤶
In modern Python, the nicest way is to use an f-string:
print(f"({x},{y},{z})")
Note how the string is prefixed with f. Everything between the curly braces {} then gets interpreted as a Python expression which is subsequently converted to a string and inserted at that point.
Note that print already follows up with a newline, so unless you want an extra one (that is, a blank line), you don't need to add \n yourself.

How to delete the last character in a string in python 3 [duplicate]

This question already has answers here:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.

"\n" not working in python when reading files [duplicate]

This question already has answers here:
Passing meta-characters to Python as arguments from command line
(4 answers)
Closed 4 years ago.
Let's say I have an example file named 'greetings.txt' with this in it
Hello\nThere
and this code
f = open("greetings.txt", "r")
readit = f.read()
print(readit)
But the output is
Hello\nThere
What do I do to make the output detect the "\n" and put Word "There" to the 2nd line?
Thanks for your answers!
Try this:
print(readit.replace(r'\n','\n'))
(When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. See here)

python strip function gives unexpected result [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.

\r or \n added in arguments list from Python [duplicate]

This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 6 years ago.
i have some scripts that take as input one or multiple paths as on argument.
The script is run like that: myScript.py D:\Folder1,E:\OtherData\Files
In the script, i split the path arguments in the comma and i read the paths.
The problem is that Python adds a \r in the end of each path for no reason.
So the script tries to read D:\Folder1\r and E:\OtherData\Files\r.
Why is this and how can i solve it?
There are many ways, here is one for example.
Replacing the relevant problematic string with an empty string:
paths= [x.rstrip() for x in paths_list]
The method rstrip() returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).

Categories