[ Python ] 基本数据类型及属性(上篇)

1. 基本数据类型

        (1) 数字 - int
        (2) 字符串 - str
        (3) 布尔值 - bool

2. int 类型中重要的方法  

   (1) int
        将字符串转换为数字类型:
    
    # 将字节为数字的字符串转换为 int 类型

# 将字节为数字的字符串转换为 int 类型
a = '123'
b = int(a)
print(type(a), a)
print(type(b), b)
	
# 用 十六进制的方式将 num 转换为十进制
num = '0011'
v = int(num, base=16)
print(v)

3. 字符串主要的方法  

  

 实例详细介绍:

  (1) capitalize()
        首字母大写

test = 'hkey'
v = test.capitalize()
print(v)

# 执行结果:
Hkey

     (2) lower() casefold()
    将字符串大写字母变小写,casefold() 可将其他国家的一些字母变小写

test = 'HkEy'
v1 = test.casefold()
v2 = test.lower()
print(v1, v2)

# 执行结果:
hkey hkey

     (3) center()
    设置宽度,并将内容居中, 20 代指总长度; *  代指空白填充

name = 'hkey'

v3 = name.center(20,'#')
print(v3)

# 执行结果:
########hkey########

     (4) count()
    在字符串中寻找子序列出现的个数

name = 'hkeyxiaoxiao'

v = name.count('x')
print(v)

# 执行结果:
2

# 可设置起始位置和结束位置

name = 'hkeyxiaoxiao'
v1 = name.count('x', 0, 8)
print(v1)

# 直接结果:
1

     (5) startswith() endswith()
  startswith():已什么序列开头,结果为布尔值
  endswith(): 以什么序列结尾,结果为布尔值

name = 'hkey'

v = name.startswith('h')
print(v)

# 执行结果:
True


v1 = name.endswith('y')
print(v1)

# 执行结果:
True

     (6) find() rfind()
    从开始往后找,找到第一个,获取其索引, 结果为: -1  表示没找到

name = 'hkeykey'

# 从开始找第一个匹配的序列,并打印序列起始的索引位置
v1 = name.find('key')
print(v1)

# 执行结果:
1

# (sub, start=None, end=None) start:起始位置 end: 结束位置
v2 = name.find('key', 0, 3)
print(v2)

# 执行结果:
-1

name = 'khkeykey'
# 从右到左查找字符索引位置
print(name.rfind('y'))

# 执行结果:
# 7

    (7) format() format_map()
    format() 格式化,将一个字符串中指定的占位符替换为值,占位符用 {} 表示 format_map() 格式化,通过字典的形式将值传给对应 key 的占位符

# 格式化,将一个字符串中指定的占位符替换为值
test = 'i am {name}, age {a}'
print(test)
# 执行结果:
i am {name}, age {a}

v = test.format(name='hkey', a=20)
print(v)
# 执行结果:
i am hkey, age 

# 可使用索引直接指定占位符
test = 'i am {0}, age {1}'
print(test)

# 执行结果:
i am {0}, age {1}

v = test.format('hkey', 20)
print(v)

# 执行结果:
i am hkey, age 20

# format_map 通过字典的形式将值传给对应 key 的占位符
test = 'i am {name}, age {a}'

v1 = test.format_map({'name': 'hkey', 'a': 20})

print(v1)
	
# 执行结果:
# i am hkey, age 20

    (8) index()
    从开始往后找,找到第一个,获取其索引, 如果没有就报错。

name = 'hkey'
v = name.index('y')
print(v)
# 执行结果:
# 3

v1 = name.index('z')
print(v1)

# 执行结果:
# Traceback (most recent call last):
#   File "E:/learn_python/day11/s2.py", line 119, in <module>
#     v1 = name.index('z')
# ValueError: substring not found

    (9) isalnum
    字符串中是否只包含 字母和数字

test = 'abcd+_'
v = test.isalnum()
print(v)

# 执行结果:
# False

test = 'abcd'
v = test.isalnum()
print(v)

# 执行结果:
# True

    (10) expandtabs
    如果字符串中含有制表符 ' \t ' ,则作为制表符来分割字符串。

s = 'username\temail\tpassword\nhkey\[email protected]\thkeyy'
v = s.expandtabs(20)
print(v)

# 执行结果:
# username            email               password
# hkey                [email protected]         hkeyy

    (11) isalpha()
    判断字符串是否包含数字,包含数字为 False,不包含数字为: True

s = 'superman'
v = s.isalpha()
print(v)

# 执行结果:
# True

    (12) isdecimal()  isdigit() isnumeric()
    判断字符串是否为数字
    isdigit() 能识别特殊符号的数字写法
    isnumeric() 能够判断中文的数字写法 ‘二’

test = '②'

v1 = test.isdecimal()
v2 = test.isdigit()
print(v1, v2)

# 执行结果:
# False True

test1 = '二'
v1 = test1.isdecimal()
v2 = test1.isdigit()
# 能够判断中文数字的写法
v3 = test1.isnumeric()
print(v1, v2, v3)

# 执行结果:
# False False True

    (13) islower()
    判断字符串小写。

test='hkey'
v=test.islower()
print(v)

#执行结果:
#True

    (14) isprintable()
    判断字符串中是否含有不可显示的字符,如 \t \n 等

test = 'abcdefg\t'
v = test.isprintable()
print(v)

# 执行结果:
# False

    (15) isspace()
    判断变量是否全部为空格

test = ' '
v = test.isspace()
print(v)

# 执行结果:
# True

    (16) istitle()  title()
    istitle() 判断是否为首字母都是大写的字符串
    title() 将字符串转换为首字母大写的标题

test = 'my heart will go on'

v = test.istitle()
v1 = test.title()
print(v)
print(v1)

# 执行结果:
# False
# My Heart Will Go On

    (17) join()
    将字符串中的每个元素按照指定的分隔符进行拼接

test = '看不见你的笑我怎么睡得着'
v = '#'.join(test)
print(v)

# 执行结果:
# 看#不#见#你#的#笑#我#怎#么#睡#得#着

    (18) ljust() rjust()
    设置宽度:
        ljust() 字符串放置左边
        rjust() 字符串放置右边

name = 'hkey'

v1 = name.ljust(20,'*')
v2 = name.rjust(20, '*')
print(v1)
print(v2)

# 执行结果:
# hkey****************
# ****************hkey

    (19) zfill()
    不能指定字符,只是 0 填充到左边

name = 'hkey'
v1 = name.zfill(20)
print(v1)

# 执行结果:
# 0000000000000000hkey

    (20) isupper() upper()
    upper() 将小写字符串转换为大写
    isupper() 判断字符串是否为大写

test = 'my heart will go on'

v1 = test.isupper()
v2 = test.upper()
print(v1)
print(v2)

# 执行结果:
# False
# MY HEART WILL GO ON

    (21) lstrip() rstrip() strip()
    lstrip() 去除字符串首部特殊符号及空格
    rstrip() 去除字符串尾部特殊符号及空格
    strip() 去除字符串首尾及空格

name = '\nhkey\n'

v1 = name.lstrip()
v2 = name.rstrip()
v3 = name.strip()
print(v1)
print(v2)
print(v3)

# 执行结果:
# v1:
# hkey
# 
# v2:
# 
# hkey
# v3:
# hkey

    (22) maketrans()
    translate() maketrans()  将两个一一对应的字符串进行替换
    translate() 替换maketrans中两个字符串

test1 = 'abcdefg'
test2 = '1234567'

v = 'adfasdfzcvdrfhkljwerto'
m = str.maketrans(test1, test2)
new_m = v.translate(m)
print(new_m)

# 执行结果:
# 1461s46z3v4r6hkljw5rto

    (23) partition()  rpartition()  split()  rsplit()
    partition() 将字符串分割为三分,并将分隔符作为独立的元素进行分割
    rpartition() 从右边开始,将字符串分割为三分,并将分隔符作为独立的元素进行分割
    split() 用指定的字符分割字符串,分割后的列表中不包含分割的字符,可执行分割次数
    rsplit() 从右边开始,用指定的字符分割字符串,分割后的列表中不包含分割的字符,可执行分割次数

test = 'asdfadfsdfxzscv'

# 将字符串分割为三分,并将分隔符作为独立的元素进行分割
v = test.partition('s')
print(v)

# 从右边开始,将字符串分割为三分,并将分隔符作为独立的元素进行分割
v1 = test.rpartition('s')
print(v1)

# 用指定的字符分割字符串,分割后的列表中不包含分割的字符,可执行分割次数
v2 = test.split('s', 1)
print(v2)
# 从右边开始,用指定的字符分割字符串,分割后的列表中不包含分割的字符,可执行分割次数
v3 = test.rsplit('s', 1)
print(v3)

# 执行结果:
# 
# v:
# ('a', 's', 'dfadfsdfxzscv')
# v1:
# ('asdfadfsdfxz', 's', 'cv')
# v2:
# ['asdfadfsdfxz', 'cv']
# v3:
# ['a', 'dfadfsdfxzscv']
# v4:
# ['asdfadfsdfxz', 'cv']

    (24) splitlines()
    分割,只能根据:True、False 是否保留换行

test = 'adfaf\nadfadf\nadfaf\n'
v = test.splitlines(True)
v1 = test.splitlines(False)
print(v)
print(v1)

# 执行结果:
# v:
# ['adfaf\n', 'adfadf\n', 'adfaf\n']
# v1:
# ['adfaf', 'adfadf', 'adfaf']

(25) startswith()  endswith()
    startswith: 以什么开头
    endswith: 以什么结尾

test = 'hkey'

# 以什么开头
v1 = test.startswith('h')
# 以什么结尾
v2 = test.endswith('e')
print(v1)
print(v2)

# 执行结果:
# True
# False

    (26) swapcase()
    大小写转换

name = 'HkEy'
v = name.swapcase()

print(v)

# 执行结果:
# hKeY

    (27) isidentifier()
    检测字符串是否是字母开头

test = '1a1dsf123'
print(test.isidentifier())

# 执行结果;
# False

  (28) replace()
  替换字符串

name = 'hkeykey'
# 将字符串中的 'k' 替换为 'f' 最多替换1次
print(name.replace('k', 'f', 1))

# 执行结果:
# hfeykey

总结:

字符串中几个常用的属性:

  join() 、 split() 、 find() 、 strip() 、 upper() 、 lower() 、lower()

4. 常用的字符串操作

(1) 通过索引获取字符

name = 'hkey'
print(name[2])

# 执行结果:
# e

(2) 切片

  通过索引的起始值、结束值、步长 来切分字符串

name = 'hkey'
v1 = name[0:2]
v2 = name[0:4:2]
print(v1)
print(v2)

# 执行结果:
# v1:
# hk
# v2:
# he

 (3) 获取字符串的长度

name = 'hkey'
print(len(name))

# 执行结果:
# 4

5. 操作字符串解析

字符串在内存中一旦创建就无法被修改,如果对字符串进行修改或者拼接,必然会生成一个新的字符串

猜你喜欢

转载自www.cnblogs.com/hukey/p/9220457.html