python - if statement with logical OR condition [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
How to check if a string contains an element from a list in Python
(8 answers)
Closed 5 years ago.
I'm a beginner in python and trying to achieve the following - need help pls...
In the below code snippet, it matches for ab but not for remaining two matches...Can anyone pls point what I'm missing.
I've a txt input which is captured in text.
for line in text:
parse = re.split('\s+', line)
print(parse)
if ("ab" or "yx" or "12") in line:
print("success")
continue
else:
print("failure")
The above code matches just "ab", it doesn't work for other matches like "yx" and "12". Don't know what I'm missing.
Thanks

I think you mean:
if "ab" in line or "yx" in line or "12" in line:

Related

How to run a statement again and again on a case but on different occurrences [duplicate]

This question already has answers here:
Replace characters in string from dictionary mapping
(2 answers)
Closed 2 years ago.
Hey guys I am making an encoding system in which each letter gets converted into predefined gibberish.
For example, 'a' has already been set as 'ashgahsjahjs'.
But using if a in data: print("ashgahsjahjs") executes this for one time only, if there are more than one A in the word, it would not print them with gibberish.
Using a while loop does not work either as it keeps printing indefinitely, so is there a way to print the gibberish each time there is a new occurrence of a letter.
you could try indexing the string.
your_string = "are you an apple?"
for i in range(len(your_string)):
if "a" == your_string[i]:
print("Found a at position {pos}".format(pos=i))
else:
print("Nope")

Python 3.x replace() with index [duplicate]

This question already has answers here:
Changing one character in a string
(15 answers)
Closed 2 years ago.
A question I came up with:
I'm trying to write a function that replaces the first and the fourth letter of a word with the same letter just capitalized.
Currently I am working with the string.replace() method. It works great for most of the time, except when there is an equal letter to the one on the fourth place before it.
Example: "bananas"
What I would expect the program to do is to return "BanAnas" but for a reason it return "BAnanas". If I use the word "submarine" it would just work fine, "SubMarine".
The code I wrote is this:
def old_macdonald(name):
name = name.replace(name[0], name[0].upper(), 1)
name = name.replace(name[3], name[3].upper(), 1)
return name
Can someone explain why this is happening?
It's because name.replace(name[3], name[3].upper(), 1) looks for the first character matching name[3]. Stop using replace altogether, chop up your string by slicing.

Split string when ";" appears [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.

python strip function gives unexpected result [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.

Trying to compare palindrome string in python [duplicate]

This question already has answers here:
How to check for palindrome using Python logic
(35 answers)
Closed 7 years ago.
I'm trying to compare a string and check if it is palindrome or not. I'm using the next method:
name = input("Enter your string")
name1 = name[-1::-1]
if(name==name1):
print("True")
else:
print("False")
but it always shows me False
has anyone idea why its not working properly?
Because you are starting at the last character of the string. You want to use name[::-1] instead. That will take the entire string from beginning to end with a step of -1, meaning that it will be reversed.

Categories