简体   繁体   English

在字符串 Python 中查找某个字符

[英]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:我几乎拥有它,但从未保存过代码大声笑....无论如何,我正在尝试查找输入字符串中间是否有某个('e')字母,例如:

  • 'Alberta' has an E in the middle at index 3 'Alberta' 在索引 3 的中间有一个 E
  • 'Edmontonian' has a T in the middle not an e 'Edmontonian' 中间有一个 T 而不是 e
  • 'Keep' has no middle char. 'Keep' 没有中间字符。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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