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
#2
did u mean bool(word[:1]) and word[:1] in 'wxyz'? else empty string is returned if word is an empty string.i'll say word[:1] in ('w','x','y','z') better in term of efficiency... less keyword/function used
. python 2.7
swallow osama bin laden
Reply
#3
yes, i meant that. i ass-u-med that and implied bool(). if i had coded that, i would quickly have discovered i was wrong.

looking back at the code which raised this question, it is the conditional of an if statement. so, it would have still worked for that purpose and i would not have learned the point you raised. so, thanks for pointing this out.
Tradition is peer pressure from dead people

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,309 Sep-27-2022, 01:38 PM
Last Post: buran
  Regex: a string does not starts and ends with the same character Melcu54 5 2,369 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,296 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,111 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Remove from end of string up to and including some character lbtdne 2 2,286 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,675 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 2,133 Dec-03-2019, 12:54 PM
Last Post: buran
  Conditional not processing as intended (Colt Steeleā€™s Udemy course) Drone4four 3 1,952 Nov-07-2019, 04:47 PM
Last Post: Drone4four
  python gives wrong string length and wrong character thienson30 2 2,945 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Find string and add character - newbi PyDK 1 2,031 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