string strip in python [duplicate] - python

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
I would need to stripoff "domain\" from "domain\name" to extract name which can be any name or the word name literally
>>> s="domain\name"
>>> x=s[5:]
>>> print(x)
n
ame
>>> s="domain\bh16"
>>> x=s[5:]
>>> print(x)
h16
>>> x=s[4:]
>>> print(x)
ih16

You can convert it to a raw string and use replace as normal
s = r"domain\bh16"
print(s.replace("domain\\", '')) #bh16

Related

Converting a Comma Separated String to a List of Strings in Python [duplicate]

This question already has answers here:
How to convert comma-delimited string to list in Python?
(7 answers)
Closed 2 years ago.
I am trying to convert a long string to a list of strings for each item separated with comma.
list = ['05-19.scl, 05-22.scl, 05-24.scl, 06-41.scl']
to :
desired_list= ['05-19.scl', '05-22.scl', '05-24.scl', '06-41.scl']
>>> listObj = ['05-19.scl, 05-22.scl, 05-24.scl, 06-41.scl']
>>> a = [x.split(',') for x in listObj][0]
>>> a
['05-19.scl', ' 05-22.scl', ' 05-24.scl', ' 06-41.scl']

Difference between a String and Print(String) [duplicate]

This question already has answers here:
What is the difference between `>>> some_object` and `>>> print some_object` in the Python interpreter?
(5 answers)
What is the difference between __str__ and __repr__?
(28 answers)
Closed 3 years ago.
>>> str4 = """This too
... is a multiline one
... built with triple double-quotes."""
>>> str4 #A
'This too\nis a multiline one\nbuilt with triple double-quotes.'
>>> print(str4) #B
This too
is a multiline one
built with triple double-quotes.
In #A and #B , we print str4 , first implicitly, then explicitly using the print function.
why the output are different.

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Convert escaped string back to original string [duplicate]

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 9 years ago.
I have a string that looks like this:
>>> st = 'aaaaa\x12bbbbb'
I can convert it to a raw string via:
>>> escaped_st = st.encode('string-escape')
'aaaaa\\x12bbbbb'
How can I convert the escaped string back to the original string? I was trying to do something like this:
escaped_st.replace('\\\\', '\\')
Decode the encoded string with the same encoding:
>>> st = 'aaaaa\x12bbbbb'
>>> escaped_st = st.encode('string-escape')
>>> escaped_st
'aaaaa\\x12bbbbb'
>>> escaped_st.decode('string-escape')
'aaaaa\x12bbbbb'

How to un-escape special characters in Python 3? [duplicate]

This question already has answers here:
Python 3 Special characters escaping
(2 answers)
Closed 9 years ago.
x='\r\n\t\t\t\t'
print(x)
The above code isn't working cos maybe of it is not recognising the special characters. So you could please help?
Use a raw string:
>>> x = r'\r\n\t\t\t\t'
>>> print(x)
\r\n\t\t\t\t
Escaping is tedious:
>>> x = '\\r\\n\\t\\t\\t\\t'
>>> print(x)
\r\n\t\t\t\t
To escape an escape sequence, you should first escape the backslash \ character. So you need to add double backslash \\ like this:
>> x = '\\r\\n\\t\\t\\t\\t'
>> print x
'\r\n\t\t\t\t'
Or you can print the raw format of the string which dumps the raw string as mentioned by jamylak
>> x = r'\r\n\t\t\t\t'
>> print '\r\n\t\t\t\t'
'\r\n\t\t\t\t'

Categories

Resources