介绍 (Introduction)

In this tutorial, we are going to discuss how we can find string length in Python. A string’s length or size is mainly required while traversing over it or while performing some operations on it.

在本教程中,我们将讨论如何在Python中找到字符串长度。 在遍历字符串或对其执行某些操作时,主要需要字符串的长度或大小。

So, now let us look at the various methods applying which we can find the length of a given string in Python.

因此,现在让我们看一下各种应用方法,这些方法可以找到Python中给定字符串的长度。

在Python中查找字符串长度的方法 (Methods to Find String Length in Python)

We can follow any one of the below-given methods to find string length in Python. We are going to discuss each one of them one-by-one.

我们可以按照以下任何一种方法在Python中查找字符串长度 。 我们将一对一地讨论它们。

1.使用len()方法 (1. Using the len() method)

Looking at the first method, and the easiest one, len() directly returns the length of the passed string object where it is called. The len() method also works on other iterable objects like lists. The below-given code illustrates how we can use the function as well as how it works.

查看第一个方法,也是最简单的方法, len()直接返回被调用的字符串对象的长度。 len()方法还适用于其他可迭代对象,如list 。 下面给出的代码说明了我们如何使用该函数以及它如何工作。


#Given string whose length is to be found
str1="Python is great!"
print("The given String is:",str1)#calling our len() method to calculate
#the length of str1
print("Length =",len(str1))

Output:

输出

String Length Using len() Method
使用len()方法的字符串长度

Here,

这里,

  • str1 is the given string,str1是给定的字符串,
  • Next, we directly call the len() method with the string and print the value returned by it. As we can see, the method gives us the right length for the given string, str1.接下来,我们直接使用字符串调用len()方法并打印其返回的值。 如我们所见,该方法为给定的字符串str1提供了正确的长度。

2.定义我们自己的函数以在Python中查找字符串长度 (2. Defining our Own Function to Find String Length in Python)

Now, coming to the next technique, we can define our own function(str_len() in our case) to calculate string length in Python.

现在,进入下一个技术,我们可以定义自己的函数(在本例中为str_len() )来计算Python中的字符串长度。

Let us see how we can do that:

让我们看看如何做到这一点:


#function that calculates length of the passed string
def str_len(str1):c=0    #counter initialised as 0for i in str1:c=c+1   #counter increased for each elementreturn c#Given string whose length is to be found
str1="JournalDev"
print("The given String is:",str1)#calling our str_len() function to calculate
#the length of str1
print("Length =",str_len(str1))

Output:

输出

String Length using User-defined function
使用用户定义函数的字符串长度

In the code above:

在上面的代码中:

  • We define a function str_len() which accepts a string and returns back the length for the same.我们定义一个函数str_len() ,它接受一个字符串并返回相同的长度。
  • Inside the function, we initialize a counter, c=0 which practically counts the number of elements in the passed string. We iterate over the string and go on incrementing the counter. Hence, by the end of the for loop, c holds the length of the string. Then, we return c.在函数内部,我们初始化一个计数器c=0 ,该计数器实际上是对传递的字符串中元素的数量进行计数。 我们遍历字符串,然后继续增加计数器 。 因此,在for循环结束时, c保留了字符串的长度。 然后,我们返回c
  • To check whether our function is working as expected we calculate and print the length of the string, str1. We can see from the output that the function returns a value 9 which is the desired output.为了检查我们的函数是否按预期工作,我们计算并打印了字符串str1的长度。 从输出中我们可以看到,该函数返回的值9是所需的输出。

结论 (Conclusion)

So, in the tutorial, we learned how we can find string length in Python. I hope it helps you understand the topic well. For any questions, feel free to use the comments below.

因此,在本教程中,我们学习了如何在Python中找到字符串长度 。 希望它能帮助您更好地理解该主题。 如有任何疑问,请随时使用以下评论。

参考资料 (References)

  • What is the max length of a python string – Stackoverflow question.python字符串的最大长度是多少 – Stackoverflow问题。

翻译自: https://www.journaldev.com/23631/find-string-length-in-python

在Python中查找字符串长度相关推荐

  1. python中查找字符串_python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  2. python3 列表长度_3种在Python中查找列表长度的简便方法

    python3 列表长度 In this article, we will be unveiling techniques to find the length of a Python list. F ...

  3. matlab 数组中查找字符串长度,Matlab 之 字符串数组查找

    Matlab的优势在于向量操作,字符串操作往往费时费力,但是如果能充分利用Matlab自带的一些函数,也可以取得不错的效果.下面就介绍一下字符串数组查找的小技巧. 字符串数组我通常会选择应用cell格 ...

  4. python输出字符串中的大写字母_如何在python中查找字符串中的大写字母

    我想在一个字符串的任何位置找到大写字母,我想如果一个字符串在一个字符串的任何位置由大写字母组成,那么应该打印字符串"1",如果字符串在任何位置不包含任何大写字母,那么应该打印字符串 ...

  5. python中复制n次字符串_在Python中查找字符串中多次出现的字符串

    精慕HU 使用正则表达式,您可以使用re.finditer查找所有(非重叠)出现的事件:>>> import re>>> text = 'Allowed Hello ...

  6. python如何判断字符串长度_Python如何查找字符串的长度?(代码示例)

    在Python中字符串是Unicode代码点的不可变序列.给定一个字符串,我们如何查找它的长度?本篇文章就来给大家介绍在Python中查找字符串长度的四种方法,希望对大家有所帮助.[相关视频教程推荐: ...

  7. 在Python中查找子字符串索引的5种方法

    在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() ...

  8. python中列表长度查询,从零开始学Python|如何在Python中查找列表的长度?

    Python中的列表是有序且可更改的集合数据类型.列表也可以有重复的条目.所述的PythonLEN()方法被用于找到任何对象的长度.在本文中,我们将按以下顺序学习如何在python中查找列表的长度: ...

  9. c ++中字符串长度的_C ++中的字符串长度

    c ++中字符串长度的 The string length in C++ can be calculated or found by various methods. Here, in this tu ...

最新文章

  1. 【Struts2学习笔记(1)】Struts2中Action名称的搜索顺序和多个Action共享一个视图--全局result配置...
  2. @所有人,CSDN 粉丝专属福利来啦!
  3. c语言fac函数求n的阶乘,急求C语言编辑题:Cnm=n!/m!(n-m)!其中n,m 由键盘输入。要求设计一个函数fac(n)求某个正整数n 的阶乘。...
  4. python三维数据转换成二维_Python科学计算全生态工具锦集
  5. myeclipse 如何显示序号
  6. Nginx服务器上安装并配置PHPMyAdmin的教程
  7. 超图预览osgb格式倾斜摄影文件
  8. 一步一步教你写带图片注释的淡入淡出插件(三)
  9. php超链接如何隐藏参数,php如何去除超链接
  10. oracle技术之顺序文件上的索引(一)
  11. 每日一题(31)—— 全局变量可不可以定义在可被多个.C文件包含的头文件中?为什么?
  12. vscode 用户配置添加 分号、保存格式化、单引号、function格式
  13. java数据实时更新_Java连载137更新数据和删除数据
  14. 20201007:力扣209周周赛题解记录(上)
  15. 图(二):图的邻接矩阵表示
  16. windows mysql 5.5 升级_windows环境mysql5.0晋级mysql5.5
  17. HTML学生个人网站作业设计——HTML+CSS+JavaScript简单的大学生书店网页制作(13页) web期末作业设计网页 web结课作业的源码 web网页设计实例作业
  18. uniapp:APP跳转小程序
  19. java项目上线流程
  20. 欧美文学与信仰传统书摘

热门文章

  1. UIPickerView用法(左右比例,整体大小,字体大小)
  2. linux下 mysql主从备份
  3. 【转】.Net 架构图
  4. win7安装证书时无响应的解决办法
  5. 数据库对象之存储过程
  6. [转载] Python函数中把列表(list)当参数
  7. [转载] python中numpy模块的around方法_更好地舍入Python的NumPy.around:舍入numpy的数组
  8. springboot 全局捕获异常
  9. Leetcode201. Bitwise AND of Numbers Range数字范围按位与
  10. Searching the Web论文阅读