len(字符串变量名) 获取长度
使用方法:
a="hello world"
print(len(a))
-------------打印结果-------------
11
切片:变量名[起始下标:结束下标:步长]
1、左闭右开原则
2、取头不取尾,头默认为1,步长默认为1
使用方法(1):
a="hello world"
print(a[2:8:1])
------------------打印结果------------------
llo wo
使用方法(2):
a="hello world"
print(a[-1:-4:-1])
---------------------打印结果---------------------
dlr
全部小写:字符串名.lower()
a="HELLO WORLD"
print(a.lower())
---------------打印结果---------------
hello world
全部大写:.upper()
a="hello world"
print(a.upper())
-------------------打印结果-------------------
HELLO WORLD
查找:.find(字符串)显示当前字符串位数
a="HELLO WORLD"
print(a.find('E'))
---------------------打印结果---------------------
1
替换:replace(旧,新,次数[选填])
a="HELLO WORLD"
print(a.replace('E','P'))
----------------------打印结果----------------------
HPLLO WORLD
index(索引/下标)---------------index(子字符串),返回数值的下标
1、使用方法:
a="hello world"
print(a.index("r"))
-----------打印结果-----------
8
2、如果不存在的话数值会报错,提示“VaLueError: substring not found”
1、格式化输出:format函数,占位符{}
1、替换占位符的时候,可以任意类型
2、字符串.format
3、有多少{},那么在format就要放多少个值,否则异常:IndexError: tuple index out of range
使用方法(1):
a="小明"
b="程序员"
c="50000"
print("深圳的{},他是个{},月薪收入是:{}".format(a,b,c))
--------------打印结果--------------
深圳的小明,他是个程序员,月薪收入是:50000
使用方法(2):
a="程序员"
b="提前下班"
print("今天是{0}节日,所有的{0}都可以{1}".format(a,b))
--------------打印结果--------------
今天是程序员节日,所有的程序员都可以提前下班
2、格式化输出:%
%s:格式化字符串
%d:格式化整数
%f:格式化浮点数
print("%s他的身上有%d,他想买%.1f的手机"%("小明",100,9.9))
-----------------------打印结果-----------------------
小明他的身上有100,他想买9.9的手机