在Python中查找字符串长度

介绍 (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:

输出

Find String Length in Python Using Len Method
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 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:

输出

Str Len To Find String Length
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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值