保定做网站设计,十大金融公司排名,做网站后台应该谁来做,免费制作图片视频的软件目录
一、输出字符串的格式
二、字符串的一些函数
1、len函数#xff1a;字符串长度
2、查找字符所在位置index
3、某字符在字符串中的个数count
4、字符切片
对字符串进行翻转 -- 利用步长
5、修改大小写字母#xff1a;
6、判断开头和结尾
7、拆分字符串 一、输出…目录
一、输出字符串的格式
二、字符串的一些函数
1、len函数字符串长度
2、查找字符所在位置index
3、某字符在字符串中的个数count
4、字符切片
对字符串进行翻转 -- 利用步长
5、修改大小写字母
6、判断开头和结尾
7、拆分字符串 一、输出字符串的格式
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.number of digitsf - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
字符串用%s
name John
print(Hello, %s! % name)
用多个参数多个参数说明符时后面%要用元组
name John
age 23
print(%s is %d years old. % (name, age))
%s也能用来表示列表
mylist [1,2,3]
print(A list: %s % mylist )
exercise:
prints out the data using the following syntax: Hello John Doe. Your current balance is $53.44.
data (John, Doe, 53.44)
format_string Helloprint(%s %s . %s, Your current balance is %s %(format_string, data[0],data[1],data[2]))
官方答案
data (John, Doe, 53.44)
format_string Hello %s %s. Your current balance is $%s.print(format_string % data)
二、字符串的一些函数
1、len函数字符串长度
astring hello world
print(len(astring))
字符串包括数字和空格因此该输出为11
2、查找字符所在位置index
astring hello world
print(astring.index(o))
输出为4意味这o与第一个字符h之间的距离为4
字符串的初始为0是由偏移量决定的 -- 第一个与第一个的偏移量为0第二个字符为1.。。
3、某字符在字符串中的个数count
astring hello world
print(astring.count(l))
4、字符切片
astring Hello world!
print(astring[3:7])
输出字符串的第3到6位没有第七位
输出结果为 lo w
如果括号中只有一个数字它将在该索引处为您提供单个字符。
如果你省略了第一个数字但保留了冒号它会给你一个从头到你留下的数字的切片。
如果你省略了第二个数字它会给你一个从第一个数字到最后的切片。
The general form is [start:stop:step]第三位是步长
astring Hello world!
print(astring[3:7:2])
对字符串进行翻转 -- 利用步长
astring Hello world!
print(astring[::-1])
5、修改大小写字母
astring Hello world!
全大写
print(astring.upper())
全小写
print(astring.lower())
6、判断开头和结尾
astring Hello world!
print(astring.startswith(Hello))
#字符串以Hello开头所以输出为true
print(astring.endswith(asdfasdfasdf)
#字符串不以该字符结尾所以输出为false7、拆分字符串
将字符串按照空格拆分为一组字符串这些字符串组合在一个列表中。
由于此示例在空格处拆分因此列表中的第一项将是“Hello”第二项将是“world”。
astring Hello world!
afewwords astring.split( )
print(afewwords)