rstrip() method for strings [duplicate] - python

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
Can someone please do this? Shouldn't the print command print There is a space. Here it is skipping as well while it should not. TIA

The argument of the method rstrip(characters) is a string of characters that will be removed from the right. That means, that the right-most char in the some_sentence string is in the set characters, it will be removed. And that's done until the right-most char is not in the set to be removed.
See https://docs.python.org/3/library/stdtypes.html?highlight=rstrip#str.rstrip
What you want to accomplish is done with .removesuffix()

Related

Equivalent Operation to b"" for an already defined string Python [duplicate]

This question already has answers here:
Best way to convert string to bytes in Python 3?
(5 answers)
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
I am able to define a bytes string as follows:
string = b"0x340xEF"
I want to cast an already made string. Let us call it string into a bytes string. How do I perform this? bytes(string) is not giving me an equivalent result.
You need to encode the string to be able to convert it to bytes:
string = "b0x340xEF".encode("latin-1")
print(string)
Output:
b'b0x340xEF'

re.match('z', 'az') returns None [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 1 year ago.
See title. The behavior here is counterintuitive. I think maybe I am missing some flags or something? Why does the regex z not match the string az?
The reason is that match only matches the beginning of a string. Must use search to do the thing that match would do in all other programming languages.
Sorry to throw shade at you Python, but you're the odd one out here.

lstrip unexpected output: removes additional character [duplicate]

This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Closed 5 years ago.
lstrip removed additional character, please help me understand it why. Is it removing all the input characters from the beginning?
'http://twitter.com/c_renwick'.lstrip('http://twitter.com/')
>>>'_renwick'
lstrip takes a list of characters to remove from the string. As c is in the list you provided, it gets removed
To achieve what you actually want, use replace:
'http://twitter.com/c_renwick'.replace('http://twitter.com/','')

Converting non-digit strings in Python [duplicate]

This question already has answers here:
Is there a way to convert number words to Integers?
(17 answers)
Closed 5 years ago.
What is the best way to parse non-digit strings e.g. "twenty two"->22 to integer or double values in python ? Especially what is the best way to detect that region if there are additional words in a sentence.
Thanks
https://github.com/akshaynagpal/w2n this has pip install
Original Comment here
https://stackoverflow.com/a/34569447

Explain this python string operation s[:6][::-1] [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I am new to python. Please anybody explain the following string operations
s="abcdefghijklmnop"
print s[:6][::-1] #is it first calculating s[:6] and then operating the result with [::-1] ?
"abcdefghijklmnop"[:6][::-1]
Take first 6 characters (abcdef).
Read the result from the end to the beginning (fedcba).
There is an other better way to get this result:
"abcdefghijklmnop"[5::-1]

Categories