python 字符串操作
来源:互联网 发布:单片机串口引脚定义 编辑:程序博客网 时间:2024/06/05 10:28
python语言字符串和列表的操作 n = 1print(str(n)+"10") print(n.__str__()+"15") #转换为字符串 str = "我爱中国aaa"--------------------常用的 print('爱' in str) #判断 爱 是否在字符串中 print(str.index('爱')) #找不到会报错 print(str.replace('爱','ai')) #字符串替换 print(str.endswith('国')) #以‘ ’结尾的字符串 存在返回 true 否则 false print(str[2:3])#输出具体的下标的字符串 print(str[2:]) # 输出具体的下标的字符串之结束 print(str*2) #两倍输出(可以为n倍)
print("hello"+"python")#字符串的拼接
Python 不支持单字符类型,单字符也在Python也是作为一个字符串使用。
Python 访问子字符串,可以使用方括号来截取字符串,如下实例:
#!/usr/bin/python3 var1 = 'Hello World!'var2 = "Runoob" print ("var1[0]: ", var1[0])print ("var2[1:5]: ", var2[1:5])
Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中
print ("我叫 %s 今年 %d 岁!" % ('小明', 10)) # 输出结果我叫 小明 今年 10 岁!
mylist=['abc','123']mylist.append('ppp')#添加mylist+=['a','b']#多个添加mylist.remove('a')#减去am=mylist.pop(0)#指定下标mylist.insert(1,'oo')#指定定下标更改mylist.clear()#删除所有del mylist[2]#删除指定下标的元素print(m)print(mylist)
阅读全文
1 0
- Python字符串操作
- Python字符串操作
- Python字符串操作
- python 字符串操作
- python字符串操作
- Python字符串操作
- Python字符串操作[转]
- Python字符串操作
- Python字符串操作
- Python学习---字符串操作
- python 中的字符串操作
- python------字符串连接操作
- python字符串操作
- python 字符串操作
- Python字符串操作
- python字符串操作
- Python字符串操作
- python 中的字符串操作
- 程序员的呐喊--读书感悟
- 【DP】TopCoder 赛题 ZigZag
- pandas.read_csv参数整理
- java3
- python学习笔记(一)
- python 字符串操作
- Python学习笔记4_选择语句与循环语句
- Training little cats
- P1090 合并果子
- fork进程
- zookeeper技术总结
- SpringMVC实现图片上传
- 递归二分搜索
- 轻院1065: 统计数字字符的个数