i = 3
if i<10:
print('hello world')
def abc(a):
if a:
return '123'
else :
return '321'
print(abc(1))
mystr.find(str, start=0, end=len(mystr))
index
跟find() 法 樣,只不過如果str不在 mystr中會報 個異常.
mystr.index(str, start=0, end=len(mystr))
count 返回 str在start和end之間 在 mystr 之間出現的次數 ![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200811161945598.png)
replace
把mystr 中的 str1 替換成 str2,如果 count 指定,則替換不超過 count 次.
mystr.replace(str1, str2, mystr.count(str1))
split
以 str 爲分隔符切⽚ mystr,如果 maxsplit有指定值,則僅分隔 maxsplit 個⼦字串
mystr.split(str=" ", 2)
capitalize
把字串的第⼀個字元⼤寫
1mystr.capitalize()
title
把字串的每個單詞⾸字⺟⼤寫
1 >>> a = "hello itcast"
2 >>> a.title()
3 'Hello Itcast'
startswith
檢查字串是否是以 指定字串開頭 , 是則返回 True,否則返回 False
str = 'hello 123 321 abc dasdadfsdf'
print(str.startswith('hello'))
endswith
檢查字串是否以指定字串結尾,如果是返回True,否則返回 False.
lower
轉換 mystr 中所有⼤寫字元爲⼩寫
upper
轉換 mystr 中的⼩寫字⺟爲⼤寫。
mystr.upper()
ljust
返回⼀個原字串左對⻬,並使⽤空格填充⾄⻓度 width 的新字串
rjust
返回⼀個原字串右對⻬,並使⽤空格填充⾄⻓度 width 的新字串
center
返回⼀個原字串居中,並使⽤空格填充⾄⻓度 width 的新字串
lstrip
刪除 mystr 左邊的空⽩字元
rstrip
刪除 mystr 字串末尾的空⽩字元
strip
刪除mystr字串兩端的空⽩字元
1 >>> a = "\n\t itcast \t\n"
2 >>> a.strip()
3'itcast'
rfind
類似於 find()函數,不過是從右邊開始查詢.
rindex
類似於 index(),不過是從右邊開始.
rpartition
類似於 partition()函數,不過是從右邊開始.
splitlines
按照 分隔,返回 個包含各 作爲元素的列表
isalpha
如果 mystr 所有字元都是字⺟ 則返回 True,否則返回 False
isdigit
如果 mystr 只包含數位則返回 True 否則返回 False
isalnum
如果 mystr 所有字元都是字⺟或數位則返回 True,否則返回 False
isspace
如果 mystr 中只包含空格,則返回 True,否則返回 False.
join
mystr 中每個元素後⾯插⼊str,構造出⼀個新的字串
第一種格式化字串 拼串
第二種格式化字串 參數傳遞
第三種格式化字串 佔位符
%s 字串佔位
%f 浮點數佔位
%d 整數佔位
第四種格式化字串 f’{變數}’/ str.format
7
len() 字串的長度
max() 最大值
min() 最小值
split()分割字串
join()拼接字串
去空格
s.strip() 左右兩邊去空格
s.lstrip() 去左邊空格
s.rstrip() 去右邊空格
字串大小寫
s.upper() 全部大寫
s.lower() 全部小寫
s.isupper() 判斷是不是大寫
s.islower() 判斷是不是小寫
HOMEWORK:
#結合當天的內容寫的
#!/user/bin/env python
# -*- coding:utf-8 -*-
# @Time : 10/8/2020 下午 7:53
# @Author : Cheney
# @File : 123.py
# @Software : PyCharm
# print(str.startswith('helli'))
# a,b = 6, 8 我想讓a=8 b=6我該怎麼辦? 2種 式實print()
# 語句練習, 兩種 式列印 hello,python
#1
a,b = 6, 8
c =a
a = b
b = c
print(a,b)
#2
a,b = 6, 8
a = a+b
b = a-b
a = a-b
print(a,b)
#3
a,b = 6,8
c = [a,b]
a= max(c)
b=min(c)
print(a,b)
#4
a,b = 6,8
str = '六'
str_a = str * a
str_b = str * b
a = len(str_b)
b = len(str_a)
print(a,b)
#5
a,b='6''8'
c=''
c = c.join(a)
a = a.join(b)
b = b.join(c)
print(a,b)
#6
a,b='6','8'
c=''
c=a
a=a.replace(a,b)
b=b.replace(b,c)
print(a,b)
#7
def changVal(num):
if num==6:
return 8
else:
return 6
a,b = 6,8
a = changval(a)
b = changval(b)
print(a,b)
# 8
a,b=6,8
a,b=b,a
print(a,b)
# 語句練習, 兩種 式列印 hello,python
#1
str = 'hello,python'
print(str)
#2
str = ''
str_List =['hello,','python']
str = str.join(str_List)
print(str)
#3
str_a,str_b ='hello','python'
print(str_a+','+str_b)