reversing a section of string in python with [start:stop:step] [duplicate] - python

This question already has answers here:
Understanding negative steps in list slicing
(2 answers)
Closed 1 year ago.
I am learning python and I can't solve this problem:
I am ok to reverse the whole string but I want to get only the "Hello" part
astring = "Hello world!"
I was expecting print(astring[0:4:-1]) would do the work but it does not.
print(astring[5:0:-1]) is better but the H is still missing. I get "olle"
Is there anyway to solve this?
Thank you,

Try this:
print(astring[4::-1])

Related

get strings between 2 delimiter in python [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I would like to get, from the following string "/path/to/%directory_1%/%directory_2%.csv"
the following list: [directory_1, directory_2]. I would like to avoid using split by "%" my string. I was hoping to find a regex that could help me. However I cannot find the correct one.
For now, I have the following:
re.findall('%(.*)%', dirty_arg)
which output ["directory_1%/%directory_2"]
Do you have any recommandation about that?
Thank you very much for your help.
Try this:
import re
regex = r"%(.*?)%"
dirty_arg = "/path/to/%directory_1%/%directory_2%.csv"
print(re.findall(regex, dirty_arg))
I've added ? to your regex which makes sure it matches as few times as possible. The output of this code is ['directory_1', 'directory_2']

getting error while using count in string "BANANA" to find "ANA" as substring using str.count() inbuild function of python [duplicate]

This question already has answers here:
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
when executing the command in python
str="BANANA"
print(str.count("ANA"))
give answer as 1.
but real answer would be 2. how to solve this
It's true that this might be misleading most tutorial sites say that count returns the number of occurences in the string, it should actually say it returns the nnumber of non-overlapping occurences in the string

Python - Is there a way to use a greater than operator that is not a comparison operator? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
I am trying to make a program that only prints positions in a string that is greater than this position in the string, first i tried the > but it comes up with "Invalid syntax" and highlights it in red. I tried the comparison greater than but that does the same thing (Because i am not comparing)
What i have tried:
sentence = ("Mark")
print (sentence[> 1])
What i want it to print:
rk
If you have any solutions or alternatives to a greater than operator in Python please let me know and i will give it a go. :)
All you need to do is print (sentence[2:])
For future reference and research, it is called slice.

Why won't this regex work? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 5 years ago.
Im try to match ip addresses and have come up with the following regular expression for python. I just cannot understand why this wont work. Any help would be greatly appreciated!
r"[0-255]\.[0-255]\.[0-255]\.[0-255]"
Because [0-255] means any char between 0 to 2 or 5. switch to something like
r"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

How to find the position of every instance of a character in a list [duplicate]

This question already has answers here:
How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)
(3 answers)
Closed 6 years ago.
I have a program where I need to identify the location of every instance of the letter A in a quote. Something like I would do with quote.index("A"), but I need every instance of A, not just the first.
I know this question has been asked before but I'm very, very new to Python and I'm having trouble understanding the answers to those questions.
If anyone could give me a dumbed down explanation of how to do this, I'd be incredibly thankful because I'm utterly lost.
If i understand correctly, you have a string and you want to keep all A's locations in e different array.
Then you can try something like that.
quote = "some quote"
locs = []
for i in range(len(quote)) :
if quote[i] == 'A' :
locs.append(i)
print(locs)

Categories