简体   繁体   中英

looking for a certain char in a string Python

So I've been trying to solve this for a while now. I've almost had it but never saved the code lol.... anyways, I'm trying to find if the input string has a certain (the 'e') letter in the middle, like:

  • 'Alberta' has an E in the middle at index 3
  • 'Edmontonian' has a T in the middle not an e
  • 'Keep' has no middle char.

how to I find the index number of a string?? and how do I find if that index is that letter?

thanks in advance!

You can check if a middle letter exists by determining whether the number of letters in the string is odd. Once that's been resolved, you can just halve the length of the string to get the index of the middle letter:

len(s) % 2 == 1 and s[len(s)//2] == "e"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM