473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to check if string is made up of only certain letters

Expand|Select|Wrap|Line Numbers
  1. sent = raw_input()
  2. if sent.__contains__("S" or "I" or "H" or "X" or "O" or "N" or "Z"):
  3.     print("YES")
  4. else:
  5.     print("NO")
This is what I have. If each letter in sent is in that list, print YES else print NO. But as long as i have 1 from the list it prints YES. That is not what I want. How can i fix this?
Oct 7 '14 #1
2 1671
bvdet
2,851 Expert Mod 2GB
You will need a loop to do that using the in keyword.
Expand|Select|Wrap|Line Numbers
  1. >>> if False in [c in sent for c in letterList]:
  2. ...     print "No"
  3. ... else:
  4. ...     print "Yes"
  5. ...     
  6. No
  7. >>>
Oct 8 '14 #2
dwblas
626 Expert 512MB
You can also use set difference and test for length
Expand|Select|Wrap|Line Numbers
  1. control_set=set(["S", "I", "H", "X", "O", "N", "Z"])
  2. test_set = set(["B", "S", "I", "H", "X", "O", "N", "Z", "A", "C"])
  3. result_set=test_set.difference(control_set)
  4. print result_set, len(result_set), "= False"
  5.  
  6. test_set = set(["S", "I", "H", "X", "O", "O"])
  7. result_set=test_set.difference(control_set)
  8. print result_set, len(result_set), "= True" 
Oct 8 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mosher | last post by:
Hi all, I was wondering if php can parse a text string for certain words and return "true" if that word is found. For example, I have a string like this: $string = "The rain in spain is the same...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
2
by: aboxylica | last post by:
I have a code in which i create a set of alphabets of specified count .Now in particular positions in it i want to embed my choice of letters how do i do it.here is my code def random_seq(): ...
9
by: JimmyJava694 | last post by:
Hey everyone, I need help trying to mutate a String. For example.. I ask the person to input a number. Then I ask them to input a String. (The string they input can be any length, but must...
5
by: Cylix | last post by:
Is there any existing method in VB.NET or any 3-third party function can find out the language in a string? Let say, isChinese? isFrench?
5
by: TOE022 | last post by:
Hi! can anyone help me with this task? im creating a palindrom : write a recursic function whitch takes a stringobject(who only contains small letters and numbers) and checks if its a...
0
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
Can you make the MaskedTextbox to only allow certain letters e.g. 10:11 A (Only allow A and P as input) ?? I don't want any other letters than A or P to appear in the text box. I mean no...
12
by: Shadlan | last post by:
Hi. I need to know if a service is running on my server. Is there any PHP instruction that I can use to do this?
0
by: Gordon | last post by:
On Oct 23, 9:43 am, Curtis <dye...@gmail.comwrote: if ($handle = fsockopen ('localhost', 80, $errnum, $errmsg, 1)) { echo ('A web server is running on this system<br>'); fclose ($handle); }...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.