Home > Mobile >  Strip (get rid of) (URL-encoding of a newline)
Strip (get rid of) (URL-encoding of a newline)

Time:12-16

Strip url

I want to strip url from a newline (%OA). On a "normal" string you can use .rstrip() or strip() method, but for url it for some reason doesn't work.

string = "Hi, Steve\n"
print(string.rstrip()) # Hi, Steve

url = "https://python.iamroot.eu/genindex.html
"
print(url)           # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip())   # "https://python.iamroot.eu/genindex.html
"
# I want to get: "https://python.iamroot.eu/genindex.html"

Do you know how to fix it?

CodePudding user response:

Maybe this is what you are looking for:

url.rstrip('
')

CodePudding user response:

you can place characters in that you want removed.

str.rstrip([chars])

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped.

See rstrip: https://docs.python.org/3/library/stdtypes.html#str.rstrip

string = "Hi, Steve\n"
print(string.rstrip()) # Hi, Steve

url = "https://python.iamroot.eu/genindex.html
"
print(url)           # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip())   # "https://python.iamroot.eu/genindex.html
"
print(url.rstrip('
'))
# I want to get: "https://python.iamroot.eu/genindex.html"
  • Related