Python练习9---string
来源:互联网 发布:win10网络连接感叹号 编辑:程序博客网 时间:2024/05/22 09:07
字符串是 Python 中最常用的数据类型,可以使用引号(‘或”)来创建字符串。具体相关操作可以去网页上学习
英文:https://www.tutorialspoint.com/python/python_strings.htm
中文:http://www.runoob.com/python/python-strings.html
1、字符串的定义和获取:使用下标
# Hello World program in Python# -*- coding: utf8 -*-import os,syssay="hello!Morning"i=0#字符串操作while i < len(say): print(i, " = ",say[i]) i=i+1
运行结果
2、使用切片方式
#!python# -*- coding: utf8 -*-import os,syssay="hello!Morning"print("len",len(say))print(say[0:5])print(say[5:])print(say[5:13])print(say[0:-7])print(say[:-7])
运行结果
3、字符串连接:+
#!python# -*- coding: utf8 -*-import os,syssay="hello!"say=say+"Morning"print("len",len(say))print(say[0:5])print(say[5:])print(say[5:13])print(say[0:-7])print(say[:-7])
运行结果
4、字符串长度获取:len
#!python# -*- coding:utf8 -*-import os,syssay="hello!"say1=say+"Morning"print("len",say1,len(say1))say2="%s%s"%(say,"Morning")print("len",say2,len(say2))say3="len %s %d"%(say2,len(say2))print(say3)
运行结果
5、字符串替换:replace
string.replace(str1, str2, num=string.count(str1))
把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.
#!python# -*- coding:utf8 -*-import os,syssay="hello!"say1=say+"Morning"print("len",say1,len(say1))say1=say1.replace("hello","Hello ")print("len",say1,len(say1))b=say1.find("Morning")c=say1[b:]print("len",c,len(c))
运行结果
阅读全文
0 0
- Python练习9---string
- Python 练习实例9
- Python练习实例9
- Python 练习实例9
- Python 练习实例9
- String练习
- Python计算机视觉编程练习8:string 模块学习
- Python练习册0-9
- Python练习100例-9
- python基础9:强化练习
- python练习
- python练习
- Python练习
- python练习
- python练习
- #python#练习
- python 练习
- 【python练习】
- SQL注入
- fd_set记录
- 鸟哥Linux学习之——数据流重定向
- TestLink学习五:TestLink1.9.13和JIRA6.3.6的集成
- 弗洛伊德论宗教的起源
- Python练习9---string
- wordcount程序
- 3192: [JLOI2013]删除物品
- TestLink学习六:TestLink1.9.13工作使用小结
- STM32硬件IIC驱动设计
- jdbc中实现模糊查询
- MVC+MVP
- 逆元以及线性逆元求法
- Python探索记(05)——if和else