在一个奇数长度的字符串中获取中间字符 [英] Getting the middle character in a odd length string

查看:343
本文介绍了在一个奇数长度的字符串中获取中间字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def get_middle_character(odd_string):
    variable = len(odd_string)
    x = str((variable/2))
    middle_character = odd_string.find(x)
    middle_character2 = odd_string[middle_character]
    return middle_character2



def main():
    print('Enter a odd length string: ')
    odd_string = input()
    print('The middle character is', get_middle_character(odd_string))

main()   

我需要弄清楚如何在给定的奇数长度字符串中打印中间字符.但是,当我运行这段代码时,我只会得到最后一个字符.问题是什么?

I need to figure out how to print the middle character in a given odd length string. But when I run this code, I only get the last character. What is the problem?

推荐答案

您需要更仔细地考虑代码的实际作用.让我们用一个例子来做这个:

You need to think more carefully about what your code is actually doing. Let's do this with an example:

def get_middle_character(odd_string):

假设我们叫get_middle_character('hello'),所以odd_string'hello':

    variable = len(odd_string)  # variable = 5

到目前为止一切都还可以.

Everything is OK so far.

    x = str((variable/2))  # x = '2'

这显然是很奇怪的第一件事-为什么要字符串'2'?那是中间字符的索引,您不只是想要一个整数吗?另外,您只需要一对括号,另一对就多余了.

This is the first thing that is obviously odd - why do you want the string '2'? That's the index of the middle character, don't you just want an integer? Also you only need one pair of parentheses there, the other set is redundant.

    middle_character = odd_string.find(x)  # middle_character = -1

很显然,您不能在odd_string中的str.find子字符串'2'中使用它,因为它永远不在那儿. str.find如果找不到子字符串,则返回-1;否则,返回-1.您应该改用str.index,当找不到子字符串时,它会为您提供清晰的ValueError.

Obviously you can't str.find the substring '2' in odd_string, because it was never there. str.find returns -1 if it cannot find the substring; you should use str.index instead, which gives you a nice clear ValueError when it can't find the substring.

请注意,即使您正在搜索中间字符,而不是中间字符的字符串化索引,您也会遇到麻烦,因为str.find给出了出现子字符串的 first 索引,这可能不是您要追求的(考虑'lolly'.find('l') ...).

Note that even if you were searching for the middle character, rather than the stringified index of the middle character, you would get into trouble as str.find gives the first index at which the substring appears, which may not be the one you're after (consider 'lolly'.find('l')...).

    middle_character2 = odd_string[middle_character]  # middle_character2 = 'o'

由于Python允许从序列末尾开始进行负索引,因此-1是最后一个字符的索引.

As Python allows negative indexing from the end of a sequence, -1 is the index of the last character.

    return middle_character2  # return 'o'

您实际上可以简化为return odd_string[middle_character],并删除了多余的分配;您仍然会有错误的答案,但是由于代码更简洁(而且没有middle_character2,这是一个糟糕的名字).

You could actually have simplified to return odd_string[middle_character], and removed the superfluous assignment; you'd have still had the wrong answer, but from neater code (and without middle_character2, which is a terrible name).

希望您现在可以看到您出了问题的地方,并且很明显应该采取的解决措施.下次使用例如 Python导师可以在此处提出问题之前调试代码.

Hopefully you can now see where you went wrong, and it's trivially obvious what you should do to fix it. Next time use e.g. Python Tutor to debug your code before asking a question here.

这篇关于在一个奇数长度的字符串中获取中间字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆