python字符串索引必须是整数_解决Python中的“ TypeError:字符串索引必须为整数”...

Here, in this article, we are going to see the possible reasons and solutions for the error “TypeError: string indices must be integers” in python.

在本文中,我们将在python中看到错误“ TypeError:字符串索引必须为整数 ”的可能原因和解决方案。

Before solving the problem let’s find out why did this error occurs.

在解决问题之前,让我们找出为什么会发生此错误。

A TypeErrorrose when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch. This exception may be raised by user code to indicate that an attempted operation on an object is not supported, and is not meant to be.

当操作或函数应用于不适当类型的对象时, TypeError上升。 关联的值是一个字符串,提供有关类型不匹配的详细信息。 用户代码可能会引发此异常,以指示不支持并且不意图支持对对象的尝试操作。

To understand the above lines, let’s take an example.

为了理解上述内容,我们举一个例子。

Let’s initialize a string in python:

让我们在python中初始化一个字符串:

example_string= 'The Crazy Programmer'

1

example_string= 'The Crazy Programmer'

1

As python is dynamically typed you need not to define its data type.

由于python是动态类型的,因此您无需定义其数据类型。

Now we will be doing two operations on our string that will give us this error and will solve the error.

现在,我们将对字符串执行两个操作,这将给我们带来此错误并解决该错误。

1. To fetch character from our string that is indexing

1.从我们正在索引的字符串中获取字符

Syntax -> variable_name[index].

语法-> variable_name [index]。

To understand the above let’s take our example.

为了理解以上内容,让我们以我们的示例为例。

example_string='The Crazy Programmer'

print(example_string['a'])

example_string='The Crazy Programmer'

print(example_string['a'])

1

2example_string='The Crazy Programmer'

print(example_string['a'])

1

2

When you run the above line, the interpreter will look for integer at the index but as we have passed the value as a string it will throw the error.

当您运行上面的行时,解释器将在索引处寻找整数,但是当我们将值作为字符串传递时,它将引发错误。

“TypeError: string indices must be integers”

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

The indexing operation requires an integer and we are passing a string.

索引操作需要一个整数,我们正在传递一个字符串。

Now take a look at the solution:

现在看一下解决方案:

example_string='The Crazy Programmer'

print(example_string[0])

example_string='The Crazy Programmer'

print(example_string[0])

1

2example_string='The Crazy Programmer'

print(example_string[0])

1

2

Output

输出量

T

Ť

example_string='The Crazy Programmer'

print(example_string[6])

example_string='The Crazy Programmer'

print(example_string[6])

1

2example_string='The Crazy Programmer'

print(example_string[6])

1

2

Output:

输出:

a

一个

As you see we get our output our problem is resolved.

如您所见,我们得到了输出,我们的问题已解决。

2. To fetch a range of character from our string that is slicing

2.从要切片的字符串中获取一定范围的字符

Syntax -> variable_name [start :end :step]

语法-> variable_name [start:end:step]

example_string='The Crazy Programmer'

print(example_string[0,2])

example_string='The Crazy Programmer'

print(example_string[0,2])

1

2example_string='The Crazy Programmer'

print(example_string[0,2])

1

2

Output:

输出:

TypeError: string indices must be integers

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

When you run the above line, the interpreter will look for integer at the start and end separated by colon (:) but as we have separated value using comma (,) it will throw the error “TypeError: string indices must be integers”.

当您运行上面的行时,解释器将在开头和结尾查找由冒号(:)分隔的整数,但是由于我们使用逗号(,)分隔了值,因此它将引发错误“ TypeError:字符串索引必须为整数”。

example_string='The Crazy Programmer'

print(example_string[0:3])

example_string='The Crazy Programmer'

print(example_string[0:3])

1

2example_string='The Crazy Programmer'

print(example_string[0:3])

1

2

Output:

输出:

The

example_string='The Crazy Programmer'

print(example_string[4:9])

example_string='The Crazy Programmer'

print(example_string[4:9])

1

2example_string='The Crazy Programmer'

print(example_string[4:9])

1

2

Output:

输出:

Crazy

Conclusion

结论

If an object is meant to support a given operation but has not yet provided an implementation, NotImplementedError is the proper exception to raise. Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.

如果对象旨在支持给定的操作但尚未提供实现,则NotImplementedError是引发的适当异常。 传递错误类型的参数(例如,当需要int时传递列表)将导致TypeError ,但是传递参数值错误(例如,预期边界之外的数字)的参数应导致ValueError。

翻译自: https://www.thecrazyprogrammer.com/2020/09/typeerror-string-indices-must-be-integers.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值