473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 1674
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
3547
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 as the rain on the plain"; I want to run a php command(s) that will parse the sentence and return true if, for example, the word "spain" is found....
20
10100
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
13844
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 check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
2
2907
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(): seq="" ch="" for i in range(0,50): ch=random.choice(("ATGC")) seq=seq+ch for i in seq:
9
4254
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 consist of only letters (A,C,G, or T) The number they input is the amount of 'random' implications applied to that String.. and replaces a letter in...
5
4321
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
3254
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 palindrom..
0
2055
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 post validation. Overriding the KeyPress function only allows me to disallow the key pressed to appear (by setting e.KeyChar = (char) 0;), but it...
12
12794
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
2899
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); } else { echo ($errnum . ' ' . $errmsg . '<br>');
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.