首页 > 编程知识 正文

TypeError:字符串索引必须是整数

时间:2023-11-26 13:13:22 阅读:309435 作者:NXYT

什么是 python 中的 TypeError?

TypeError 是 python 编程语言中的异常之一。当对不支持的对象类型执行操作时,或者可以说不是有效的对象类型时,会出现此异常。

  • 每当出现异常时都会引发或发生。
  • 此外,顾名思义,只要有错误的对象,就会出现这种类型的错误,我们会对其执行不相关的操作。
  • Python 编程语言中的错误主要有三种类型:语法错误、逻辑错误、和异常。
  • 这种类型的错误属于异常错误的范畴。
  • 用户可以使用 raise 关键字轻松处理TypeError

TypeError:字符串索引必须是整数

  • 众所周知,字符串的基本结构由索引值组成。
  • 字符串的起始值从索引值 0 开始,它将持续到给定字符串的长度。
  • 每当我们想要取出具有指定索引值的字符串的特定字符时,就会出现TypeError。不过,我们将输入不同对象类型的另一个值,而不是在索引中输入整数值。
  • 例如,如果我们想从字符串中获取字符 2,我们将索引作为整数而不是整数输入,这将引发TypeError字符串索引必须是整数。

让我们借助例子来理解它:

TypeError示例:字符串索引必须是整数

示例 1:给定一个名为“JavaTpoint”的字符串,您需要从中取出特定的字符“T”。T3】

(这里,我们将讨论上述示例中出现TypeError的所有情况及其解决方案。)

情况 1:我们以字符串格式传递数字,而不是在索引中传递整数值。


# Python program to fetch the particular character from the string

print("Enter a string: ")                                      # Enter string JavaTpoint
s=input()
print("To fetch out the character 'T'  from the string")
s["4"]                                                              # As the string begans with index value 0,        
# after onwards it will go further by adding 1 to it.

解说:

在上面的例子中,如果我们观察到这一点,它会产生一个TypeError:字符串索引必须是整数,因为要从字母表 JavaTpoint 中取出字符“T”,我们必须以整数形式而不是字符串形式传递索引值 4。

这里,我们传递值 4,但不是以整数的形式,而是以字符串的形式,正如我们在双引号中赋予它的那样。

情况 1 的输出:

借助下面提到的更正,让我们详细了解一下:

修正案例 1:


# Python program to fetch the particular character from the string

print("Enter a string: ")                                      # Enter string JavaTpoint
s=input()
print("To fetch out the character 'T'  from the string")
s[4]                                                              # As the string begins with index value 0,        
# after onwards it will go further by adding 1 to it.      

修正情况 1 的输出:

情况 2:我们传递索引的字符串值,而不是整数值。


# Python program to fetch the particular character from the string

print("Enter a string: ")                                      # Enter string JavaTpoint
s=input()
print("To fetch out the character ' o '  from the string")
s['o']                                                              # As the string begans with index value 0,        
# after onwards it will go further by adding 1 to it.

解说:

在上面的例子中,如果我们观察到这一点,它会产生一个TypeError:字符串索引必须是整数,因为在这里,要从字母表 JavaTpoint 中取出字符“o”,我们必须以整数形式而不是字符串形式传递索引值 6。

在上面的程序中,我们直接传递字母表而不是整数值,而是以字符串的形式传递,就像我们在双引号中赋值一样。

借助下面提到的更正,让我们详细了解一下:

情况 2 的输出:

修正案例 2:


# Python program to fetch the particular character from the string

print("Enter a string: ")                                      # Enter string JavaTpoint
s=input()
print("To fetch out the character ' o '  from the string")
s[6]                                                              # As the string begins with index value 0,        
# after onwards it will go further by adding 1 to it.

修正情况 2 的输出:

例 2:给定一个名为“奇妙”的字符串,要求你从该字符串中取出特定的切片,或者可以从中说出字符串“der”的一部分,我们称之为字符串切片。T3】

情况 1:我们以字符串格式传递数字,而不是在索引中传递整数值。


# Python program to fetch the particular part from the string

print("Enter a string: ")                                      # Enter string Wonderful
s=input()
print("To fetch out the particular slice ' der '  from the string")
s["3":"6"]                                                              # As the string begans with index value 0,        
# after onwards it will go further by adding 1 to it.

解说:

在上面的例子中,它产生了一个TypeError:字符串索引必须是一个整数,因为,在这里,为了从字母奇妙中取出字符串“der”的特定部分,我们必须以整数形式而不是字符串形式传递索引值 3: 6。

在上面的程序中,我们需要传递值 3: 6,不是以整数的形式,而是以字符串的形式,因为我们已经用双引号赋予了它。

情况 1 的输出:

借助下面提到的更正,让我们详细了解一下:

修正案例 1:


# Python program to fetch the particular part from the string

print("Enter a string: ")                                      # Enter string Wonderful
s=input()
print("To fetch out the particular slice ' der '  from the string")
s[3:6]                                                              # As the string begans with index value 0,        
# after onwards it will go further by adding 1 to it.

修正情况 1 的输出:

例 3:举一个字典的例子,我们给出了一个字典集,要求我们从字典中取出特定的关键字及其对应的值。T3】

情况 1:当我们在索引中传递字符串值,而不是整数值


# Python program to fetch out the particular key with its corresponding value

d1 = { "Shivani" : 19, "Sneha" : 31, "Preeti" : 8, "Abhi" : 50}   # declaring a dictionary
for i in d1:
  print("Sneha: " + i["Sneha"])
  print("Abhi: " + i["Abhi"])

在上面的例子中,它产生了一个TypeError:字符串索引必须是一个整数,因为这里要从字典 d1 中取出特定的关键字“Sneha”,我们必须传递索引 I,它已经是整数形式而不是字符串。

在上面的程序中,我们直接传递字符串而不是整数值,而是以字符串的形式传递,就像我们在双引号中赋值一样。

情况 1 的输出:

借助下面提到的更正,让我们详细了解一下:

修正案例 1:


# Python program to fetch out the particular key with its corresponding value

d1 = { "Shivani" : 19, "Sneha" : 31, "Preeti" : 8, "Abhi" : 50}   # declaring a dictionary
for i in d1:
  print(i)

修正情况 1 的输出:

处理TypeError:字符串索引必须是整数

在上面的例子中,我们看到当我们以非整数值的错误格式输入字符串时,可能会产生TypeError。虽然我们已经看到了所有例子的修正,但是我们必须知道如何处理所有这样的异常。

为了在 python 编程语言中处理这些异常,我们通常会使用“try”和“except”关键字 try block。我们将输入所有的测试用例,主要是尽可能多,其中这是产生TypeError的可能性。我们将通过打印用户定义的错误消息,在 except block 中输入处理上述异常的方式。

让我们借助一个例子更详细地了解

例:


# Python program to handle the type error exception

l1 = ["Apple", "Mango", "Banana", "Grapes"]             # declaring a list
indices = [0, 1, 2, "2", 3]
for i in range(len(indices)):
    try:
        print(l1[indices[i]])                                        # case which generates TypeError
    except TypeError:
        print("TypeError: Check list of indices")

以上异常处理示例的输出:


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。