python字符串长度教程,如何获取Python字符串长度?

We programmers generally use string data in our applications. While using the string data one of the most used operation is getting the size or length of the string. In this tutorial we will look different methods to get the length of the string.

我们的程序员通常在我们的应用程序中使用字符串数据。 使用字符串数据时,最常用的一种操作是获取字符串的大小或长度。 在本教程中,我们将寻找不同的方法来获取字符串的长度。

使用镜头功能 (Using Len Function)

Python have build in function named len which will provide the length of the given string. We will just provide the string variable name or string directly to the len function like below.

Python具有名为len内置函数,该函数将提供给定字符串的长度。 我们只将字符串变量名或字符串直接提供给len函数,如下所示。

myname="poftut.com"

len(myname)

#10

len("poftut.com")

#10

Using Len Function

使用镜头功能

使用SizeOf函数(Using SizeOf Function)

Another way to get the length of a string is using sys library provided sizeof function. sizeof actually provide the size as byte.

获取字符串长度的另一种方法是使用sys库提供的sizeof函数。 sizeof实际上以字节为单位提供大小。

import sys

sys.getsizeof(myname)

#59

sys.getsizeof("poftut.com")

#59

Using SizeOf Function

使用SizeOf函数

用For计数(Counting with For)

Another and last way is counting the given string by using while or for iterations by ourself. As we know strings are just a list of characters so we can iterate over these characters. While iterating we can count the characters. This type of count operation provides some flexibility like skipping some characters and do not add total count. We will use following for loop.

另一种也是最后一种方法是通过使用while或自己for迭代来计数给定的字符串。 我们知道字符串只是字符列表,因此我们可以遍历这些字符。 在迭代时,我们可以计算字符数。 这种类型的计数操作提供了一些灵活性,例如跳过某些字符并且不增加总计数。 我们将使用以下for循环。

count=0

for ch in myname:

count=count+1

print(count)

#10

Counting with For

用For计数

We can create a simple function like below.

我们可以创建一个简单的函数,如下所示。

def strcount(mystr):

count=0

for ch in mystr:

count=count+1

return count

strcount(myname)

10

Counting with For

用For计数

.u2e1ec2270de59a459bfc062f3c608b4c , .u2e1ec2270de59a459bfc062f3c608b4c .postImageUrl , .u2e1ec2270de59a459bfc062f3c608b4c .centered-text-area { min-height: 80px; position: relative; } .u2e1ec2270de59a459bfc062f3c608b4c , .u2e1ec2270de59a459bfc062f3c608b4c:hover , .u2e1ec2270de59a459bfc062f3c608b4c:visited , .u2e1ec2270de59a459bfc062f3c608b4c:active { border:0!important; } .u2e1ec2270de59a459bfc062f3c608b4c .clearfix:after { content: ""; display: table; clear: both; } .u2e1ec2270de59a459bfc062f3c608b4c { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .u2e1ec2270de59a459bfc062f3c608b4c:active , .u2e1ec2270de59a459bfc062f3c608b4c:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .u2e1ec2270de59a459bfc062f3c608b4c .centered-text-area { width: 100%; position: relative; } .u2e1ec2270de59a459bfc062f3c608b4c .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .u2e1ec2270de59a459bfc062f3c608b4c .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .u2e1ec2270de59a459bfc062f3c608b4c .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .u2e1ec2270de59a459bfc062f3c608b4c:hover .ctaButton { background-color: #E67E22!important; } .u2e1ec2270de59a459bfc062f3c608b4c .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .u2e1ec2270de59a459bfc062f3c608b4c .u2e1ec2270de59a459bfc062f3c608b4c-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .u2e1ec2270de59a459bfc062f3c608b4c:after { content: ""; display: block; clear: both; }

LEARN MORE  Php - Operators

.u2e1ec2270de59a459bfc062f3c608b4c , .u2e1ec2270de59a459bfc062f3c608b4c .postImageUrl , .u2e1ec2270de59a459bfc062f3c608b4c .centered-text-area { min-height: 80px; position: relative; } .u2e1ec2270de59a459bfc062f3c608b4c , .u2e1ec2270de59a459bfc062f3c608b4c:hover , .u2e1ec2270de59a459bfc062f3c608b4c:visited , .u2e1ec2270de59a459bfc062f3c608b4c:active { border:0!important; } .u2e1ec2270de59a459bfc062f3c608b4c .clearfix:after { content: ""; display: table; clear: both; } .u2e1ec2270de59a459bfc062f3c608b4c { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .u2e1ec2270de59a459bfc062f3c608b4c:active , .u2e1ec2270de59a459bfc062f3c608b4c:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .u2e1ec2270de59a459bfc062f3c608b4c .centered-text-area { width: 100%; position: relative; } .u2e1ec2270de59a459bfc062f3c608b4c .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .u2e1ec2270de59a459bfc062f3c608b4c .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .u2e1ec2270de59a459bfc062f3c608b4c .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .u2e1ec2270de59a459bfc062f3c608b4c:hover .ctaButton { background-color: #E67E22!important; } .u2e1ec2270de59a459bfc062f3c608b4c .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .u2e1ec2270de59a459bfc062f3c608b4c .u2e1ec2270de59a459bfc062f3c608b4c-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .u2e1ec2270de59a459bfc062f3c608b4c:after { content: ""; display: block; clear: both; }

了解更多信息-运营商

翻译自: https://www.poftut.com/get-python-string-length/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值