码迷,mamicode.com
首页 > 其他好文 > 详细

基本数据类型三

时间:2018-08-23 22:14:30      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:tran   存在   拼接   数据类型   slow   分割   sde   com   exit   

str

  1. 断句:expandtabs
#断句 expandtabs
test = "用户名\t密码\t邮箱\n李飞\t123\tlifei@163.com\n李飞\t123\tlifei@163.com\n李飞\t123\tlifei@163.com\n李飞\t123\tlifei@163.com\n"
v = test.expandtabs(20)
print(v)
print(test)
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
用户名                 密码                  邮箱
李飞                  123                 lifei@163.com
李飞                  123                 lifei@163.com
李飞                  123                 lifei@163.com
李飞                  123                 lifei@163.com

用户名	密码	邮箱
李飞	123	lifei@163.com
李飞	123	lifei@163.com
李飞	123	lifei@163.com
李飞	123	lifei@163.com


Process finished with exit code 0

  2、判断字符串是否全部为数字,isalnum

test = "123_"
v = test.isalnum()
print(v)

  3.判断字符串中是否全部为英文字母或者汉字 isalpha

test = "apple苹果"
v = test.isalpha()
print(v)

  4.判断字符串中内容是否为数字

test = "二"
v = test.isdecimal() #仅识别数字
r = test.isdigit() #识别特殊字符 ②
j = test.isnumeric()# 识别汉字的数字
print(v,r,j)

  5.是否存在不可显示的字符,例如:\n  \t

test = "abscddd\ndddd\t"
v = test.isprintable()
print(v)

  6.判断字符串中是否全部为空格,是为true  否则为false

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

  7.判断字符串是否为标题,并转换成标题。

test = "we are family"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)

  8.将字符串中每一个元素按照指定分隔符进行拼接

test = "abcdefghigh"
v = "_".join(test)
print(v)
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
a_b_c_d_e_f_g_h_i_g_h

Process finished with exit code 0

  9.判断字符串中是否为大/小写,并全部转换为大/小写

test = "aBCdefgh"
v1 = test.islower()
v2 = test.lower()
print(v2.islower())
v3 = test.isupper()
v4 = test.upper()
print(v4.isupper())
print(v1,v2,v3,v4)
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
True
True
False abcdefgh False ABCDEFGH

Process finished with exit code 0

  10.移除字符串中指定字符元素(字符串从左边/右边移除指定元素时,左边/右边起始位置必须为该元素才可以移除)

test = "abchabchhabc"
v1 = test.lstrip("abc")
v2 = test.rstrip("abc")
v3 = test.strip("abc")
print(test)
print(v1)
print(v2)
print(v3)
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
abchabchhabc
habchhabc
abchabchh
habchh

Process finished with exit code 0

  

  11.对应关系替换

test = "abddd;ajdlalddlldld;sakiiepwpj"
m = str.maketrans("aeiou","12345")
new_test = test.translate(m)

print(new_test)

  12.分割为三部分

test = "abadks\njkdjalbskjaljdkalbk\nsjldalbklal"
v = test.partition(‘b‘)
print(v)
v1 = test.rpartition("b")
print(v1)
v2 = test.split("b",4)
print(v2)
v3 = test.splitlines(True)
print(v3)
v4 = test.splitlines()
print(v4) 
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
(‘a‘, ‘b‘, ‘adks\njkdjalbskjaljdkalbk\nsjldalbklal‘)
(‘abadks\njkdjalbskjaljdkalbk\nsjldal‘, ‘b‘, ‘klal‘)
[‘a‘, ‘adks\njkdjal‘, ‘skjaljdkal‘, ‘k\nsjldal‘, ‘klal‘]
[‘abadks\n‘, ‘jkdjalbskjaljdkalbk\n‘, ‘sjldalbklal‘]
[‘abadks‘, ‘jkdjalbskjaljdkalbk‘, ‘sjldalbklal‘]

Process finished with exit code 0

  12.以“”开头,以“”结尾

test = "abcef123"
v = test.endswith("a")
print(v)
v1 = test.startswith("a")
print(v1)
E:\project\venv\Scripts\python.exe E:/project/day1/s3.py
False
True

Process finished with exit code 0

  13.大小写转换

test = "abaCddd"
v = test.swapcase()
print(v)

  14.判断是否为标识符

test = "def"
v = test.isidentifier()
print(v)

  15.将指定字符替换为指定字符串

test = "1dddsdsdssffffgggg"
v = test.replace("ds","12",2)
print(v)

  

 

基本数据类型三

标签:tran   存在   拼接   数据类型   slow   分割   sde   com   exit   

原文地址:https://www.cnblogs.com/panwj/p/9524855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!