首页 > 其他分享 >phthon字符与list常用方法和属性

phthon字符与list常用方法和属性

时间:2023-03-05 23:33:38浏览次数:44  
标签:字符 mypty demo list str print phthon method

字符

methods :

  1. count:统计字符在字符串中出现的次数(return int)

[searchStr,startIndex,endIndex]

mypty = 'This is a demo of the count method of str'
print(mypty.count('i', 0, len(mypty))) # 2
# 找不到则返回0
print(mypty.count('z', 0, len(mypty)))  # 0
  1. find 在字符串里找到第一个符合查找条件的字符的下标(return int) 

[searchStr,startIndex,endIndex]

mypty = 'This is a demo of the find method of str'    
print(mypty.find('i', 0, len(mypty))) # 2  
# 找不到则返回-1
print(mypty.find('z', 0, len(mypty)))  # -1
  1. replace 在字符串中替换指定的字符 (return new str)

[beforeStr,afterStr,replaceCount] replaceCount:默认全部替换

mypty = 'This is a demo of the replace method of str'
newMypty = mypty.replace('is', 'are')
print(newMypty) # Thare are a demo of the replace method of str
  1. upper & isupper : 将字符转换为全大写 & 字符内是否都是大写

upper : (return upper str)
isupper : return Boolean

mypty = 'this is a demo of the upper method of str'
print(mypty.upper()) # THIS IS A DEMO OF THE UPPER METHOD OF STR
newMypty = mypty.upper()
print(newMypty.isupper()) # True
  1. lower & islower : 将字符转换为全小写 & 字符内是否都是小写

lower : (return lower str)
islower : return Boolean

mypty = 'THIS IS A DEMO OF THE LOWER METHOD OF STR'  
print(mypty.lower()) # this is a demo of the lower method of str
newMypty = mypty.lower()
print(newMypty.islower()) # True
  1. split : 将字符基于指定字符分割成一个list,没有参数则默认以空格为分割符

Return a list of the words in the string, using sep as the delimiter string.

[splitKey,splitCount]
return list

mypty = 'this is a demo of the split method of str'
print(mypty.split(' ')) # 基于空格分割 ['this', 'is', 'a', 'demo', 'of', 'the', 'split', 'method', 'of', 'str']

  1. strip : 去除一个字符前后两边的指定字符,不传参则去除两边空格(一侧有也可以替换,不穿参数则只能替换两侧空格)

如果想要替换中间的空格符,则可以使用str.replace(' ','')实现

Return a copy of the string with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

return stripped str

# 没有参数
mypty = '   this is a demo of the split method of str    '
print(mypty ) # '   this is a demo of the split method of str    ' # 初始有两边空格
print(mypty.strip()) # 'this is a demo of the split method of str' 无两边空格

#有参数
mypty = 'zzzthis is a demo of the split method of strzzz'
print(mypty) # 'zzzthis is a demo of the split method of strzzz'
print(mypty.strip('zzz')) # 'this is a demo of the split method of str'

#错误参数,则返回原字符
print(mypty.strip('sss')) # 'zzzthis is a demo of the split method of strzzz'

8.格式化输出

  1. %s

语法:str%s%{value} 其中s指占位符 ,后面的百分比后的大括号标识s对应的值

允许为数字、字符、字l典、list类型

demostr = '致%s'%('爱丽丝')
print(demostr) # 致爱丽丝
demostr = '致%s'%(1>2)
print(demostr) # 致False
demostr = '致%s'%([1,2,3])
print(demostr) # 致[1, 2, 3]

  1. %d

语法: 只接收数字类型%d%(Number)

# 整型
numberStr = '只接受数字类型%d'%(269)
print(numberStr) # 只接受数字类型269
# 浮点型
doubleStr = '只接受数字类型且输出为整数(向下取整)%d'%(99.9)
print(doubleStr) # 只接受数字类型且输出为整数(向下取整)99

# 其他类型则会报错
numberStr = '只接受数字类型%d'%('哈哈哈')
print(numberStr) # TypeError
  1. %f

语法:`只接受数字类型%f'%(Number) 其中整型会保留六位小鼠

floatStr = '只接受数字类型%f'%(269)
print(floatStr) # 只接受数字类型269.000000
#有小数的依然保留六位
floatStr = '只接受数字类型%f'%(269.9)
print(floatStr) # 只接受数字类型269.000000
#超过六位,看第七位,第七位大于5则第六位进1且舍去剩余位数
## 进1
floatStr = '只接受数字类型%f'%(269.1111116)
print(floatStr) # 只接受数字类型269.111112
## 舍去
floatStr = '只接受数字类型%f'%(269.1111115)
print(floatStr) # 只接受数字类型269.111111  
  1. F/f 表达式
name = 19
mystr = F'我的名字是{name}'
print(mystr) # 我的名字是19

未完待续

以上。

标签:字符,mypty,demo,list,str,print,phthon,method
From: https://www.cnblogs.com/hjk1124/p/17182191.html

相关文章

  • salesforce零基础学习(一百二十六) Picklist Value Set 优缺点和使用探讨
    本篇参考:https://help.salesforce.com/s/articleView?id=sf.fields_creating_global_picklists.htm&type=5当我们创建Picklist字段时,比如很多表很多字段都会用到同样的p......
  • Python学习笔记(七)字符串操作
    一、数据类型转换%s字符串%d数值整数%f浮点数formatf表达式挖坑填坑法则1name='张三'2age=183height=180.545print('我的名字是%s'%name)6p......
  • 剑指 Offer50. 第一次只出现一次的字符
    题目描述  解法一哈希表法思路:首先遍历一遍s,在哈希表里统计字符数量是否大于1;再遍历一遍s,在哈希表中找到首个数量为1的字符classSolution{public:char......
  • Java小练习--统计字符串中字母和数字的个数
    题目统计字符串中字母和数字的个数完整代码packagetestimportjava.util.Scanner;publicclasscustomer1{publicstaticvoidmain(Stringargs[]){System.out.......
  • stata:删除字符串的空格
    //替换所有空格//下面语句结果为4displength(subinstr("1233","","",.))//itrim(s)将字符间多于一个空格缩减为一个空格,对首尾空格不起作用displengt......
  • NOI / 1.7编程基础之字符串 12:加密的病历单
    描述小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会。在药房实习期间,小英扎实的专业基础获得了医生的一致好评,得知小英在计算概论中取得过好成绩后,主任又......
  • 1.7编程基础之字符串
    12:加密的病历单1.描述小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会。在药房实习期间,小英扎实的专业基础获得了医生的一致好评,得知小英在计算概论中取得......
  • STATA:字符型 数值型变量转换
    STATA:字符型数值型变量转换//real()将字符转为数字gena6=price+real("1")//将变量a6的类型设置为int(原来浮点型)recastinta6//将变量a6的类型由int转为strtos......
  • JavaSE——ArrayList集合练习
    packagecom.zhao.test2;publicclassPhone{privateStringlogo;privateIntegerprice;publicPhone(){}publicPhone(Stringlogo,I......
  • APP学习5(ListView和Adapter)
    1.ListViewListView是以列表的形式展示数据内容,并且能够根据列表的高度自适应屏幕显示。   2.常用数据适配器(Adapter)BaseAdapter是基本的适配器,实际上是一个抽......