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!
Related
This question already has answers here:
Check if something is (not) in a list in Python
(4 answers)
Closed 2 months ago.
What I mean by that is, that I want to take a list, let's say"
["i","am","you","pure","fact"],
then check whether this list contains 2 different strings, like "i" and "pure", and then if true, it runs something.
This is what I am looking for, but in PYTHON.
I don't know how to do this sort of check.
Try this out
elements: list = ["i","am","you","pure","fact"]
str1: str = "i"
str2: str = "pure"
if str1 in elements and str2 in elements:
# Execute code
...
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I want to figure out how to take the integer from a input when there is both a integer and string.
For example, if I input "hello 3", is there a way I can separate the "3" to another variable aside from the "hello"?
Would this work for you:
myInput=input() # Get Input
myString,myIntStr=myInput.split(" ") # Split in to list based on where the spaces are
myInt=int(myIntStr) # Convert to int
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
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)))
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I want to print all the characters in a string as a list but for each character to be printed once even if recurring. So far I have:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
symbolsx.append(i)
This prints every character, even if the character is repeated.
symbolsx = list(set(symbolsx))
First pass the list to set function to remove duplicates, then reverted that set back to list by passing it to list function.
How about:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
if i not in symbolsx:
symbolsx.append(i)