False when check for upper later in python [duplicate] - python

This question already has answers here:
How can I test if a string starts with a capital letter? [duplicate]
(3 answers)
Closed 2 years ago.
I need to check if the first letter of a list is upper. For that I wrote this simple code, where obviasly my word "Try" starts with capital "T":
h=[]
h.append("Try")
a = str(h[0])
print(a)
print(a.isupper())
BUT when I print a.isupper I get always False. Should I convert the variable in something, or it should be str object? How can I solve this problem

You dont need the list. Just do:
a= "Try"
print(a[0].isupper())

You are checking the whole string to be capital, that's why it's False.
When you do print(a[0].isupper()), it checks if the whole string(Try in your case) is capital. Hence, it returns False.
You want to check just the first letter of the string, so do this instead:
In [615]: print(a[0].isupper())
True
Where a[0] gives you T.

You are using h[0] which gives "Try", and when you check for a.isupper()
It has both lower case and upper case , please check for a[0] , then you get true if the first letter is capital

Related

Checking if a number is present in string [duplicate]

This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 2 years ago.
I have a working function that takes an input as string and returns a string. However, I want my function to return an empty string ("") if the input contains any number in whatever position in the string.
For example :
>>> function("hello")
works fine
>>> function("hello1")
should return ""
The main thing you need for that is the method "isdigit()" that returns True if the character is a digit.
For example:
yourstring="hel4lo3"
for char in yourstring:
if char.isdigit():
print(char)
Will output 4 and 3.
I think it is a good exercise for you trying to do your code from that!

Python comparing a list with set to find pangram [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
I'm trying to find if a string is a Pangram. My approach is to get the string to have unique letters by using the set method. Then using the string.ascii as the base alphabet. I find out after some testing that if I try to compare the 2 with the 'in' operator. Some of the letters get passed over and won't be removed from the alphabet list.
def is_pangram(sentence):
uniqueLetters = set(sentence.lower().replace(" ", ""))
alphabet = list(string.ascii_lowercase)
for letter in alphabet:
if letter in uniqueLetters:
alphabet.remove(letter)
if len(alphabet) <= 0:
return True
return False
print(is_pangram("qwertyuiopasdfghjklzxcvbnm"))
this example will compare 13 letters and the rest will not. Anyone can point me in the right direction? Am I misunderstanding something about set?
Perhaps the following does what you want:
import string
target = set('qwertyuiopasdfghjklzxcvbnm')
all((k in target for k in set(string.ascii_lowercase)))

How does the count function count? [duplicate]

This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
How does the count() method work? [duplicate]
(1 answer)
Closed 4 years ago.
This is a short one, yet very irritating. I know I can count the amount of times a string occurs within another string like this:
'banana'.count('a')
>>>3
meaning that banana contains the letter "a" 3 times.
This is where it gets kind of weird.
My first confusion is - when I do 'foo'.count(''), what does Python look for?
is '' == None == anything?
It doesn't seem to be the case, but then again, what IS '' logically speaking? And more importantly, why does
'test'.count('')
>>>5
return one more than the length of the string?
What the hell is included in a string that's always 1 higher than the amount of letters? the void?
EDIT: the ' character twice looks like one " character. I am talking about two times ' here, to avoid confusion
EDIT2: There seems to be some confusion about how the amount of '' happen. Refer to comments below.
Every string1 can be thought of as:
any_string = "" + "".join(any_string) + ""
which contains exactly len(any_string) + 1 instances of ''.
For "foo" for example, it would be:
"" + "f" + "" + "o" + "" + "o"+ ""
# |----- from join -------|
As it can be seen there are 4 instances of "" in it.
Note however, that this is a problem where no answer or all answers could somehow support a case for themselves. It get's philosophical:
How much nothing is contained in nothing?
How much nothing is contained in something?
This answer tries to explain the convention used by Python and does not intend to suggest that this is the way all languages do it \ should be doing it; it is just how Python does it.
1Empty strings are an exception and are handled differently; they simply return 1; which is yet another convention.
str.count(sub)
Counts the number of occurrences of sub in str.
Since strings are sequences, it basically counts the number of splits sub would cause in str.
An empty string is at the beginning, between each character, and at the end.
Hence, why when you use 'test', which has a len of 4, you get 5 occurrences of sub ('').

Avoid overlap when using replace [duplicate]

This question already has answers here:
Python replace function [replace once]
(6 answers)
Replace 2 characters within a reversed string at once
(2 answers)
Closed 4 years ago.
I'm playing around with a simple crypting program, and I want it to work so that every specific symbol, always will turn into another specific symbol. That might not be the best description, but I don't know how else to put it... For an example:
"a" is going to be "h"
"A" is going to be "k"
"h" is going to be "W"
text_1 = "aAh"
text_2 = text_1.replace('a', 'h')
text_3 = text_2.replace('A', 'K')
text_4 = text_3.replace('h', 'W')
print text_4
#the output is "WKW"
#I need the output to be "hKW"
The problem is, that I'm using the replace-command for every single symbol-replacement, so if we suppose that the codes are typed in the same order as our example, and the message I want to crypt is "aAh", then I would like the crypted output to be "hKW" but actually we get this output instead "WKW".
I know why this is happening, so my question is:
How do I get the program to crypt the message the way I intended it to do?
The problem you have is that you're applying your changes to intermediate strings (so the previous changes will affect the result)
Consider trying to calculate the intended changes on the initial string for each character and build the final string after.
You can use a dict for your character mapping and then use a generator expression to translate the characters:
m = {'a': 'h', 'A': 'K', 'h': 'W'}
print(''.join(m.get(c, c) for c in text_1))
This outputs:
hKW

Python vowel check [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 years ago.
I know there are a few ways to remove vowels, but when I try the method below, the output I get is the string just being printed len(string) times. For example:
s="Labido Labidi"
for i in s:
if i.lower()=='a' or 'e' or 'i' or 'o' or 'u':
s=s.replace(i,"")
print(s)
The resultant output is:
Labido Labidi
Labido Labidi
...and so on
What's going on inside the loop ? It isn't even going through the if statement.
You are using the or statement incorrectly:
s="Labido Labidi"
for i in s:
if i.lower()=='a' or i.lower()=='e' or i.lower()=='i' or i.lower()=='o' or i.lower()=='u':
s=s.replace(i,"")
print(s)
You need to put your full evaluational statement after the or
The problem is your if logic.
After the first or you have to repeat i.lower() == 'e', etc.
Try this:
s="Labido Labidi"
for i in s:
if i.lower() in 'aeiou':
s=s.replace(i,"")
print(s)
The problem is your if condition. or connects two boolean expressions; it doesn't work the same as in English. What you need to check is
if i.lower()=='a' or
i.lower()=='e' or
...
Better yet, just make a single check on a list of vowels this way:
if lower() in "aeiou":
DETAILS
Any expression used as a Boolean value is evaluated according to whether it is "truthy" or "falsey". Non-Boolean data types are sometimes not obvious. In general, zero and `None` values are "falsey", and everything else is "truthy".
Thus, each of the single letters is "truthy", so the Python interpreter regards your if statement as if it says
if i.lower()=='a' or True or True or True or True:
In short, this is always True; your program thinks that everything is a vowel.

Categories