網(wǎng)站備案密碼收不到做網(wǎng)絡(luò)營銷推廣的公司
知識回顧
1、默認情況下,input函數(shù)接收的數(shù)據(jù)是字符串類型。
2、字符串類型的關(guān)鍵詞是str。
3、\n和\t都是轉(zhuǎn)義字符,\n用來換行,\t用來留出一段固定長度的空白。
4、type函數(shù)能夠用來查看變量的數(shù)據(jù)類型
5、數(shù)據(jù)類型的轉(zhuǎn)換,舉個例子
x_str='3.14'
print(x_str)
y_int=float(x_str)
print(y_int)
第一個是字符型、第二個是浮點類型。
6、有字符串 'a good student',如何取出其中的good字符串
s='a good student'
print(s[2:6])
一、字符串的操作函數(shù)
通過索引可以取出字符串當中的某些內(nèi)容,除了取得字符串中的內(nèi)容之外,是否還能夠?qū)ψ址銎渌僮髂?#xff1f;?????????
如果某個字符串s1在另一個字符串s2中出現(xiàn)過,那么我們稱s1是s2的子串。
a good student-子串-good
1.字符串操作函數(shù)之len
Xiaotuzi 和 Maotouying 誰的名字的拼寫長度比較長?
要測量某個字符串的長度,可以使用len函數(shù)
Xiaotuzi長度為8
Maotouying長度為10
Maotouying 名字的拼寫長度比Xiaotuzi名字的拼寫長度長。
2.len函數(shù)的使用方式:len(參數(shù))
如果參數(shù)是字符串, 那么該函數(shù)會返回字符串的長度
練習:
編程實現(xiàn)求出'Xiaotuzi' 和 'Maotouying' 兩個名字的長度
name1='Xiaotuzi'
name2='Maotouying'
print(len(name1))
print(len(name2))
3.字符串操作函數(shù)之find
- I am a good student
- 怎么判斷這段話中是否含有g(shù)ood這個單詞?
- 要查找個字符串是否是另一個字符串的子串,可以使用find函數(shù)
- good 是 I am a good student的一個子串。
(1).find函數(shù)的使用方式:字符串.find(參數(shù))
find函數(shù)可以查找字符串中是否含有某個子串, 如果有,則返回第一個子串的位置, 否則返回-1,此處的參數(shù)可以是要查找的子串。
編程實現(xiàn)判斷'good'是否為'I am a good student'的子串
I | a | m | a | g | o | o | d | s | t | u | d | e | n | t | ||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
第一次出現(xiàn)子串的位置:7
s='I am a good student'
t='good'
print(s.find(t))
(2).I am a good student
怎么判斷這段話中是否含有bad這個單詞呢?
s='I am a good student'
t='bad'
print(s.find(t))
4.字符串操作函數(shù)之replace
I do not like Python
怎么把這段話中的do not替換成really這個單詞?
要將字符串中的子串替換成另一個字符串,可以使用replace函數(shù)
I do not like Python
替換do not 為 really
I really like Python
替換后的字符串為'I really like Python'。
4.1、replace函數(shù)的使用方式: 字符串.replace(參數(shù)1,參數(shù)2)????????
replace函數(shù)可以將字符串中的子串替換成另一個字符串,其中參數(shù)1是要被替換的子串,參數(shù)2是新的要替換子串的字符串。 它的返回值是子串被替換后的新的字符串。
s='I do not like Python'
print(s.replace('do not','really'))
5.字符串操作函數(shù)之strip
' ? ? happy day ? ? '
怎么把這段話中的前后空白部分給去掉?
- 要去掉一個字符串的前后空白部分,可以使用strip函數(shù)
- 空白部分 happy day?空白部分
- 去掉空白部分????????
- 替換后的字符串為'happy day'。
5.1、strip函數(shù)的使用方式:字符串.strip( )
strip函數(shù)可以將字符串中的前后空白部分去掉,此處沒有參數(shù)。 它的返回值是字符串被去掉前后空白部分后的新的字符串
s=' happy day '
print(s.strip())
二、字符串的格式化輸出
如何把變量按照一定的格式輸出?
比如 a = 3, 輸出:'變量的值是:3'
這就需要用到 格式化符號來實現(xiàn) 格式化輸出!
1.什么是格式化?
格式化:按照一定的格式把字符串進行輸出
在Python中,可以在字符串中添加格式化符號,使用運算符%來實現(xiàn)格式化輸出,常用的格式化符號有:%d、%f 、%s
1.1字符串的格式化輸出 - %d
%d是整數(shù)格式化符號,可以將字符串中對應的部分替換成整型變量a的值
a=3
print('變量的值是:%d' %a)
格式化符號 一定要和變量 配合使用嗎?
答:不一定, 可以不用和變量配合使用
print('I am %d years old'%9)
有時我們需要特定的格式。
%03d、%3d、%-3d, 可以讓數(shù)字占3個空格。
%03d :如果不足3位,左邊用0補齊
%3d :如果不足3位,左邊用空格補齊
%-3d:如果不足3位,右邊用空格補齊
print('%03d'%6)
print('%3d'%6)
print('%-3d'%6)
2.字符串的格式化輸出 - %f
%f是浮點數(shù)格式化符號,可以將字符串中對應的部分替換成浮點數(shù)
print('pi=%f'%3.1415926)
如果需要特定格式, %.2f 可以保留小數(shù)點后2位
%6.2f 可以保留小數(shù)點后2位,同時讓結(jié)果占6個格,左邊用空格補齊
print('pi=%.2f'%3.1415926)
print('pi=%6.2f'%3.1415926)
剛剛介紹了保留小數(shù)點后若干位輸出的一種方法,實際上,還有另外一種方法也可以用來控制小數(shù)點位數(shù)的輸出。
3.字符串的格式化輸出 - %s
%s是字符串的格式化符號,可以將字符串中對應的部分替換為字符串
print('I am a %s'%'boy')
4.字符串的格式化輸出
字符串中可以使用多個格式化符號
print('My name is %s,and I am %s years old'%('Tom',10))
練習:
1.打印一份乘法表:
print('%4d * %d =%d'%(1,1,1))
print('%4d * %d =%d'%(1,2,2),end='')
print('%4d * %d =%d'%(2,2,4))
print('%4d * %d =%d'%(1,3,3),end='')
print('%4d * %d =%d'%(2,3,6),end='')
print('%4d * %d =%d'%(3,3,9))
練習02:
軍隊攔截了一條敵軍發(fā)送的密碼信息,內(nèi)容是:
password = 'C++ is the best computer programming language in the world.'
請對這個字符串做以下處理:
- 輸出密碼的長度
- 判斷這個字符串是否包含 ‘programming’ 這部分內(nèi)容,如果有輸出它在字符串中第一次出現(xiàn)的位置,如果沒有這部分內(nèi)容,輸出-1
- 把'C++'這部分內(nèi)容替換為'Python'
password='C++ is the best computer programming language in the world'
print(len(password))
print(password.find('programming'))
print(password.replace('C++','Python'))
總結(jié):
- 字符串常用操作函數(shù)
- 格式化占位符
- 保留小數(shù)位數(shù)輸出
所有代碼
x_str='3.14'
print(x_str)
y_int=float(x_str)
print(y_int)
s='a good student'
print(s[2:6])
name1='Xiaotuzi'
name2='Maotouying'
print(len(name1))
print(len(name2))
s='I am a good student'
t='bad'
print(s.find(t))
s='I do not like Python'
print(s.replace('do not','really'))
s=' happy day '
print(s)
print(s.strip())
a=3
print('變量的值是:%d' %a)
print('I am %d years old'%9)
print('%03d'%6)
print('%3d'%6)
print('%-3d'%6)
print('pi=%f'%3.1415926)
print('pi=%.2f'%3.1415926)
print('pi=%6.2f'%3.1415926)
pi='3.1415926'
k=4
print('%.*f' %(k,pi))
print('I am a %s'%'boy')
print('My name is %s,and I am %s years old'%('Tom',10))
print('%4d * %d =%d'%(1,1,1))
print('%4d * %d =%d'%(1,2,2),end='')
print('%4d * %d =%d'%(2,2,4))
print('%4d * %d =%d'%(1,3,3),end='')
print('%4d * %d =%d'%(2,3,6),end='')
print('%4d * %d =%d'%(3,3,9))
password='C++ is the best computer programming language in the world'
print(len(password))
print(password.find('programming'))
print(password.replace('C++','Python'))
“學而不思則罔,思而不學則殆?!?br /> 這句話強調(diào)了學習與思考相結(jié)合的重要性,只有將學習和思考相互配合,才能真正理解知識、掌握學問,避免陷入迷茫或陷入空想。
感謝大家支持!