Python Forum
testing if a character in a string is an intended one
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing if a character in a string is an intended one
#1
previously i had asked which was better: word[0] in ('w','x','y','z') vs word[0] in 'wxyz'. but now i have a new variation on the problem. the value for word might be an empty string so i need to change word[0] to word[:1] to avoid an exception i don't want to happen. that means i could be doing the test word[:1] in 'wxyz' with an empty string which would always give a True value for the in operation. so i can't use that one at all and apparently must use word[:1] in ('w','x','y','z') to perform a correct test. the only alternative i can imagine is word[:1] and word[:1] in 'wxyz'.

so, now, between word[:1] in ('w','x','y','z') and word[:1] and word[:1] in 'wxyz' which is the best choice? or is there something even better that i have overlooked?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
testing if a character in a string is an intended one - by Skaperen - Feb-26-2018, 03:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,377 Sep-27-2022, 01:38 PM
Last Post: buran
  Regex: a string does not starts and ends with the same character Melcu54 5 2,427 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,408 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,182 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 2,340 May-17-2020, 09:24 AM
Last Post: menator01
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,741 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 2,169 Dec-03-2019, 12:54 PM
Last Post: buran
  Conditional not processing as intended (Colt Steeleā€™s Udemy course) Drone4four 3 1,998 Nov-07-2019, 04:47 PM
Last Post: Drone4four
  python gives wrong string length and wrong character thienson30 2 3,002 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Find string and add character - newbi PyDK 1 2,069 May-15-2019, 01:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020