介绍 (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:
输出 :
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 0
for i in str1:
c=c+1 #counter increased for each element
return 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:
输出 :
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 thefor
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