[ python ] 字符串的操做及做業題

字符串的操做方法

capitalize() : 首字母大寫python

s1 = 'my heart will go on'
print(s1.capitalize())  # 首字母大寫

# 執行結果:
# My heart will go on
capitalize

 

upper() : 所有大寫git

s1 = 'my heart will go on'
print(s1.upper())   # 所有轉爲大寫

# 執行結果:
# MY HEART WILL GO ON
upper

 

lower() : 所有小寫api

s1 = 'my heart will go on'
print(s1.lower())   # 所有轉爲小寫

# 執行結果:
# my heart will go on
lower

 

 這裏寫一個使用大小寫轉換的實例,驗證碼的比較:ide

auth_code = 'KDwq'

user_code = input('請輸入驗證碼(不區分大小寫):')
if user_code.upper() == auth_code.upper():      # 將驗證碼和用戶輸入的字符統一轉換爲大寫進行比較;
    print('驗證成功.')
else:
    print('驗證失敗.')

 

 swapcase() : 大小寫反轉spa

s1 = 'MY HEART will go ON'
print(s1.swapcase())    # 大小寫反轉;大寫轉換爲小寫,小寫轉換爲大寫

# 執行結果:
# my heart WILL GO on
swapcase

 

 

 title(): 標題首字母大寫3d

s1 = 'MY HEART will go ON'
print(s1.title())   # 標題首字母大寫,用空格隔開的每一個單詞都看成一個標題處理
# # 執行結果:
# My Heart Will Go On
title

 

 

 center(): 居中,默認填充空格code

s1 = 'MY HEART will go ON'
print(s1.center(30, '#'))   # 居中顯示,第一個參數是位寬int類型,第二個參數是佔位符,str類型;

# 執行結果:
# #####MY HEART will go ON######
center

 

 

 startswith(): 以什麼開頭,返回 bool 值orm

s1 = 'nice to meeting you'
print(s1.startswith('ni'))  # 判斷以什麼字符串開始,返回bool類型,正確返回 True,錯誤返回 False

# 執行結果:
# True

print(s1.startswith('no'))

# 執行結果:
# False
startswith

 

 

endswith(): 以什麼結尾,返回 bool 值blog

s1 = 'nice to meeting you'
print(s1.endswith('you'))   # 判斷以什麼字符串結尾,返回bool類型,正確返回 True,錯誤返回 False

# 執行結果:
# True

print(s1.endswith('yyy'))

# 執行結果:
# False
endswith

 

 

find() 和 index() 都是經過元素找索引的方法,區別以下:索引

find()方法:元素存在打印索引,元素不存在返回 -1

index()方法:元素存在打印索引,元素不存在報錯。錯誤信息:ValueError: substring not found

s1 = 'nice to meeting you'

print(s1.find('to'))

# 執行結果:
# 5

print(s1.find('xxx'))

# 執行結果:
# -1

print(s1.index('to'))

# 執行結果:
# 5

# print(s1.index('xxx'))

# 執行結果:
# ValueError: substring not found
find 和 index

 

 

因此,在程序中應當使用 find() 來查找索引,儘可能避免程序出現報錯信息

 

strip(): 默認去除行首和行尾的空格;
    rstrip() 從左刪
    lstrip() 從右刪

s1 = '    nice to meeting you    '

print(s1.strip())   # 默認去除開頭和結尾的空格部分;

# 執行結果:
# nice to meeting you

s1 = '#######nice to meeting you#######'
print(s1.strip('#'))    # 第一個參數爲開頭和結尾要去除的字符

# 執行結果:
# nice to meeting you
strip

 

 

count():統計字符串出現個數

s1 = 'adfasdfasdfasdfertadf'
print(s1.count('a'))    # 統計字符串出現的次數

# 執行結果:
# 5
count

 

 

split(): 按照指定字符分隔成多個元素  str --> list

s1 = 'nice to meeting you'
print(s1.split())   # 默認將字符串經過空格分隔並組成列表類型

# 執行結果:
# ['nice', 'to', 'meeting', 'you']
split

 

 

format 有三種用法:

print('我叫{}, 跟我念一遍:{}, 年齡:{}, 愛好:{}'.format('clot', 'clot', 20, '攝影'))
print('我叫{0}, 跟我念一遍:{0}, 年齡:{1}, 愛好:{2}'.format('clot', 20, '攝影'))
print('我叫{name}, 跟我念一遍:{name}, 年齡:{age}, 愛好:{hobby}'.format(name='clot', age=20, hobby='攝影'))

# 執行結果:
#
# 我叫clot, 跟我念一遍:clot, 年齡:20, 愛好:攝影
# 我叫clot, 跟我念一遍:clot, 年齡:20, 愛好:攝影
# 我叫clot, 跟我念一遍:clot, 年齡:20, 愛好:攝影
format

 

 

 replace(): 字符串替換

s1 = 'nice to meeting you'
print(s1.replace('meeting', 'see')) # 在字符串中,用第一個參數替換掉第二個參數,默認是匹配上則所有替換

# 執行結果:
# nice to see you
replace

 

 

isdigit(): 判斷字符串是否所有是數字組成

s1 = '123123123123'
print(s1.isdigit()) # 判斷字符串是否所有由數字組成,小數點沒法匹配上,返回bool類型;

# 執行結果:
# True
isdigit

 

 

isalpha():判斷字符串是否全是字母組成

s1 = 'asdfasdf'

print(s1.isalpha()) # 判斷字符串是否所有由字母組成,返回bool類型;

# 執行結果:
# True
isalpha

 

 

isalnum():判斷字符串是否由數字和字母組成

s1 = 'asdfa234asdf'

print(s1.isalnum()) # 判斷字符串是否所有由字母或字母組成,返回bool類型;

# 執行結果:
# True
isalnum

 

 

做業題

 

1. 判斷下列邏輯語句的 True、False

1) 1 > 1 or 3 > 4 or 4 > 5 and 2> 1 and 9 > 8 or 7 < 6
2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2

2. 求出下列邏輯語句的值。

1) 8 or 3 and 4 or 2 and 0 or 9 and 7
2) 0 or 2 and 3 and 4 or 6 and 0 or 3
3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5

3. 下列結果是什麼?
1) 6 or 2 > 1
2) 3 or 2 > 1
3) 0 or 5 < 4
4) 5 < 4 or 3
5) 2 > 1 or 6
6) 3 and 2 > 1
7) 0 and 3 > 1
8) 2 > 1 and 3
9) 3 > 1 and 0
10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

4. 寫代碼:計算 1-2+3...+99 中除了88意外全部數的總和?

 

如下 F 表示: False, T 表示:True

1. 判斷下列邏輯語句的 True、False
1) 1 > 1 or 3 > 4 or 4 > 5 and 2> 1 and 9 > 8 or 7 < 6
計算過程:
# 1 > 1 or 3 > 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# 1 > 1 or 3 > 4 or F and 9 > 8 or 7 < 6
# 1 > 1 or 3 > 4 or F or 7 < 6
# F or F or 7 < 6
# F or 7 < 6
# F

2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
計算過程:
# F and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F or F and 9 > 8 or 7 < 6
# F or F or 7 < 6
# F or 7 < 6
# F

3) 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
計算過程:
# 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
# F or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
# F or F or 9 < 8 and 4 > 6 or 3 < 2
# F or F or F or 3 < 2
# F or F or 3 < 2
# F or 3 < 2
# F


2. 求出下列邏輯語句的值。

1) 8 or 3 and 4 or 2 and 0 or 9 and 7
計算過程:
# 8 or 3 and 4 or 2 and 0 or 9 and 7
# 8 or 4 or 0 or 7
# 8 or 0 or 7
# 8 or 7
# 8

2) 0 or 2 and 3 and 4 or 6 and 0 or 3
計算過程:
# 0 or 2 and 3 and 4 or 6 and 0 or 3
# 0 or 3 and 4 or 6 and 0 or 3
# 0 or 4 or 6 and 0 or 3
# 0 or 4 or 0 or 3
# 4 or 0 or 3
# 4 or 3
# 4


3) 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
計算過程:
# 5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
# 9 or 10 and 2 or 3 and 5 or 4 or 5
# 9 or 2 or 3 and 5 or 4 or 5
# 9 or 2 or 5 or 4 or 5
# 9 or 5 or 4 or 5
# 9 or 4 or 5
# 9 or 5
# 9

3. 下列結果是什麼? 
提示:
(1)牢記優先級:not > and > or2)and 取值
    
    從前日後,
    (a) 第一個數的bool值爲 False,那就直接返回第一個數(0就返回0,False就返回False);
    (b) 若是第一個數的bool值爲 True,第二個數的bool值爲 False,則返回第二個數的值;
    (c) 若是兩個數的bool值都爲True,則返回第二個數.
    (d) 若是兩個數的bool值都爲False,則返回第一個數.

(3)or 取值
    從前日後,
    (a) 第一個數的bool值爲 False,那就直接返回第二個數;
    (b) 若是第一個數的bool值爲 True,第二個數的bool值爲 False,則返回第一個數的值;
    (c) 若是兩個數的bool值都爲True,則返回第一個數.
    (d) 若是兩個數的bool值都爲False,則返回第二個數.
    

1) 6 or 2 > 1
計算過程:
print(6 or 2 > 1)
# 6 or 2 > 1
# 6 or T
# 6

2) 3 or 2 > 1
計算過程:
print(6 or 2 > 1)
# 6 or 2 > 1
# 6 or T
# 6

3) 0 or 5 < 4
計算過程:
print(0 or 5 < 4)
# 0 or 5 < 4
# 0 or F
# False

4) 5 < 4 or 3
計算過程:
print(5 < 4 or 3)
# 5 < 4 or 3
# F or 3
# 3

5) 2 > 1 or 6
計算過程:
print(2 > 1 or 6)
# 2 > 1 or 6
# T or 6
# T

6) 3 and 2 > 1
計算過程:
print(3 and 2 > 1)
# 3 and 2 > 1
# 3 and T
# T

7) 0 and 3 > 1
計算過程:
print(0 and 3 > 1)
# 0 and 3 > 1
# 0 and T
# 0

8) 2 > 1 and 3
計算過程:
print(2 > 1 and 3)
# 2 > 1 and 3
# T and 3
# 3

9) 3 > 1 and 0
計算過程:
print(3 > 1 and 0)
# 3 > 1 and 0
# T and 0
# 0

10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
計算過程:
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
# 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
# T and 2 or 2 < 3 and 3 and 4 or 3 > 2
# 2 or 2 < 3 and 3 and 4 or 3 > 2
# 2 or F and 3 and 4 or 3 > 2
# 2 or F and 4 or 3 > 2
# 2 or F or 3 > 2
# 2 or 3 > 2
# 2 or T
# 2




4. 寫代碼:計算 1-2+3...+99 中除了88意外全部數的總和?

num = 0
count = 0
while count < 100:
    count += 1      # count = count + 1 必須寫在 continue 以前,不然進入死循環
    if count == 88:
        continue
    if count % 2:    # 獲取奇數
        num += count
    else:
        num -= count
print(num)
做業題答案及分析過程
相關文章
相關標籤/搜索