Python總結 基礎知識 2

2021-10-02 11:10:59 字數 3382 閱讀 9446

# 修改字串首字母的大小寫

s1 = 'hello'

print(s1) #結果為hello

print(s1.capitalize()) #結果為hello

# s1[0] = 'h' 唯讀的,會丟擲異常

s1 = s1[0:1] + s1[1].upper() + s1[2:]

print(s1) #結果為hello

s2 = 'hello'

s = s2[0].lower() + s2[1:]

print(s) #結果為hello

# 修改字串中每乙個單詞的首字母變成大寫

s3 = 'hello world'

print(s3.capitalize()) #結果為hello world

arr = s3.split(' ')

new_str = f' '

print(new_str) #結果為hello world

new_str2 = arr[0].capitalize() + ' ' + arr[1].capitalize()

print(new_str2) #結果為hello world

s1 = '12345'

print('12345是數字:',s1.isdigit()) #結果為true

print(int(s1)) #結果為12345

s2 = '12345a'

print('12345a是數字:',s2.isdigit()) #結果為false

print('12345a是字母數字混合形式:',s2.isalnum()) #結果為true

s3 = '12_345a'

print('12_345a是字母數字混合形式:',s3.isalnum()) #結果為false

print(" ".isspace()) #結果為true

print("12.45".isdecimal()) # 檢測字串中是否為整數,結果為false

print("abc3d".isalpha()) #結果為false

# 第二題:如果將字串轉換為整數,如何做才安全

s1 = "1234"

print(int(s1)) #結果為1234

s2 = '1234a'

#print(int(s2))

if s2.isdigit():

print(int(s2))

else:

print('s2不是數字,無法轉換')

try:

print(int('223aaa'))

except exception as e:

print('223aaa不是數字,無法轉換')

print(e)

s1 = 'abcde'

s2 = ""

for c in s1:

s2 = c + s2

print(s2) #結果為edcba

# [a:b:c] 變數[頭下標:尾下標:步長]

print(s1[::1]) #結果為abcde

print(s1[::2]) #結果為ace

print(s1[::-1]) #結果為edcba

# 格式化整數

n = 1234

print(format(n, '10d')) #結果為 1234

print(format(n, '0>10d')) #結果為0000001234

print(format(n, '0<10d')) #結果為1234000000

# 格式化浮點數

x1 = 1234.56789

x2 = 30.1

print(format(x1, '0.2f')) # 保留小數點後兩位1234.57

print(format(x2,'0.2f')) # 結果為30.10

# 題目3:描述format函式

print(format(x2,'*>15.4f')) # 右對齊,********30.1000

print(format(x2, '*<15.4f')) # 左對齊,30.1000********

print(format(x2, '*^15.4f')) # 中心對齊,****30.1000****

print(format(123456789,',')) # 用千位號分隔,123,456,789

print(format(1234567.865565656,',.2f')) # 1,234,567.87

print(format(x1,'e')) #結果為1.234568e+03

print(format(x1,'0.2e')) #結果為1.23e+03

# 輸出單引號和雙引號

print('hello "world"') #結果為hello "world"

print("hello 'world'") #結果為hello 『world『

print('"hello" \'world\'') #結果為hello 『world『

# 讓轉義符失效(3種方法: r、repr和\)

print(r'let \'s go!') #結果為let \'s go!

print(repr('hello\nworld')) #結果為'hello\nworld'

print('hello\\nworld') #結果為hello\nworld

# 保持原始格式輸出字串

print("""

hello

world

i love you.

""")

結果為

hello

world

i love you.

# 用逗號分隔輸出的字串

print("aa","bb",sep=";") #結果為aa;bb

# 如何讓print不換行

print('hello',end='')

print('world') #結果為helloworld

# 格式化

s = 'road'

x = len(s)

print('the length of %s is %d' % (s,x)) #結果為the length of road is 4

python總結–基礎知識-3

Python 基礎知識2

1.類新增新屬性和新屬性賦值 metaclass type class rectangle def init self self.width 0 self.height 0 def setattr self,name,value if name size size property value se...

python基礎知識(2)

1.變數和按引用傳遞 在pyhton中對變數賦值時,你其實是在建立物件的引用。2.動態引用和強型別 python中的物件引用沒有與之相關聯的型別的資訊 即python可以自動判斷所定義的型別不需要進行型別宣告 而隱式轉換只是在很明顯的情況下才會發生。可以用type 檢視變數的型別,也可以用isins...

Python基礎知識(2)

在程式語言中,注釋的作用是為了讓自己或他人更快地了解程式作者的思路和意圖,提高 的可讀性。同時在多人協同開發時,也可以提高開發效率。特備說明 注釋部分不參與 的編譯執行。單行注釋主要應用於對某個變數,等的簡短說明,不能換行,只能在一行內應用。多行注釋主要運用於大段文字的說明,可以換行使用,一般用於對...