remove t and n from string python code example
Example 1: get rid of n in string python
a_string = a_string.rstrip("\n")
Example 2: remove n characters from string python
example_string = "Hello there"
def remove_chars(n, string):
list_of_chars_in_string = [char for char in string]
for num in range(n):
list_of_chars_in_string.pop()
new_string = ''.join(list_of_chars_in_string)
return new_string
Example 3: python remove \t
>>> s="abc \n \t \t\t \t \nefg"
>>> ''.join(s.split())
'abcefg'
>>> ''.join(c for c in s if not c.isspace())
'abcefg'
import re
s = 'abc \n \t \t\t \t \nefg'
re.sub(r'\s', '', s)
=> 'abcefg'