Python 常用数据类型的内置函数与方法(int、string)

1、数字类型的常用方法

1.1 int()方法将数字字符串转换为数字

以下是 int() 方法的语法:

int(x,base=10)

参数
  • x -- 字符串或数字。
  • base -- 进制数,默认十进制。

返回值

返回整型数据(结果一定是十进制数,base只是在说明字符串中的进制)。

例1:

name = "1234"
n = int(name)           #缺省上一行双引号里内容为十进制,十进制时,可以不写第二个参数:base=10
print(type(name),name)
print(type(n),n)

  输出结果:

<class 'str'> 1234
<class 'int'> 1234

 例2.

name = "41"
n = int(name,base=16)
print(type(name),name)
print(type(n),n)

  输出结果:

<class 'str'> 41
<class 'int'> 65

 1.2 bit_length()方法:返回当前整数的二进制的bit位数(至少或最少用几位二进制数来表示

  
n = 15
v=n.bit_length()
print(v)

  输出结果为:4      #用二进制来表示十进制数15,最少要用4位二进制数来表示,即:1111

n = 3
v=n.bit_length()
print(v)

  输出结果为:2       #用二进制来表示十进制数3,最少要用4位二进制数来表示,即:11

 

2、字符串类型的常用方法

2.1  capitalize()方法:实现字符串首字母大写

test="hello world!"
new = test.capitalize()
print(test)
print(new)

  输出结果为:

hello world!
Hello world!

2.2 string.casefold()和string.lower()都可以将大写变为小写。

区别在于:

对 Unicode 的时候用 casefold

string.casefold官方说明:

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'casefold()converts it to "ss".

The casefolding algorithm is described in section 3.13 of the Unicode Standard

 

lower() 只对 ASCII 也就是 'A-Z'有效,但是其它一些语言里面存在小写的情况就没办法了。文档里面举得例子是德语中'ß'的小写是'ss'

s = 'ß' s.lower() # 'ß' s.casefold() # 'ss'

 

name = "aBcDeFg"
v1 = name.casefold()
v2 = name.lower()
print(v1)
print(v2)

 

  运行结果为:

abcdefg
abcdefg

 2.3 center() 方法返回一个指定的宽度 width 的字符串,且给定字符串居中,fillchar 为两边填充的字符,默认为空格。

 语法

center()方法语法:

str.center(width[, fillchar])
  • width -- 字符串的总宽度。
  • fillchar -- 填充字符。

返回一个指定的宽度 width自身居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

 例1:

name = "www.yulinu.edu.cn"
new = name.center(40)
print(new)

  运行结果:

           www.yulinu.edu.cn            

 例2:

name = "www.yulinu.edu.cn"
new = name.center(40,"*")
print(new)

  运行结果为:

***********www.yulinu.edu.cn************

 2.4  count() 方法用于统计字符串里某个字符或子串出现的次数。可选参数为在字符串搜索的开始与结束位置。

count()方法语法:

str.count(sub, start= 0,end=len(string))
  • sub -- 搜索的子字符串
  • start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
  • end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

该方法返回子字符串在字符串中出现的次数。

name = "students"
n = name.count("s")
print(n)

  运行结果为:2

name = "studentsstudentsstudents"
n = name.count("ts")
print(n)

  运行结果为:3

name = "studentsstudentsstudents"
n = name.count("ts",3,10)
print(n)

  运行结果为:1

 2.5 endswith()和startswith()

作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型

相关函数:判断字符串开头 startswith()

语法:string.endswith(str, beg=[0,end=len(string)])
          参数说明:

string: 被检测的字符串
str:      指定的字符或者子字符串(可以使用元组,会逐一匹配)
beg:    设置字符串检测的起始位置(可选,从左数起)
end:    设置字符串检测的结束位置(可选,从左数起)
如果存在参数 beg 和 end,则在指定范围内检查,否则在整个字符串中检查  
 返回值:如果检测到字符串,则返回True,否则返回False。

name = "sdflsdjflds.jpg"
stat = name.endswith("jpg")
if stat == True:
    print(name,"是一个图片文件")

  运行结果为:

sdflsdjflds.jpg 是一个图片文件

  

name = "sdflsdjflds.jpg"
stat = name.endswith("jpg,beg = 4,end = 10")
if stat == True:
    print(name,"是一个图片文件")
else:
    print("没有找到")

  运行结果:

没有找到

  

 

转载于:https://www.cnblogs.com/kinderboy/p/10233489.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值