//
Python
Search
Try Notion
Python
Print Multiple Line
print(str1,str2,sep=('\n'))
Formate Output
Python
Copy
# Python > 2.6 txt3 = "My name is {}, I'm {}".format("John",36) txt2 = "My name is {0}, I'm {1}".format("John",36) print("For only {price:.2f} dollars!".format(price = 49)) #python > 3.6 name = 'Tushar' age = 23 print(f"Hello, My name is {name} and I'm {age} years old.")
数字
格式
输出
描述
3.1415926
{:.2f}
3.14
保留小数点后两位
3.1415926
{:+.2f}
+3.14
带符号保留小数点后两位
Try Exception
Python
Copy
try: print(x) except: print("An exception occurred")
Raise Error
Python
Copy
if not type(x) is int: raise TypeError("Only integers are allowed")
Check Value Type
Python
Copy
# View Value Type type(i) # Check Value Type isinstance(i, int)
Python
Copy
# 16 进制 int int(x, 16)
Python String
Python
Copy
# str.upper() "This is a 'string'.".upper() # "THIS IS A 'STRING'." #str.lower() "This IS a 'string'.".lower() # "this is a 'string'. #str.capitalize() "this Is A 'String'.".capitalize() # Capitalizes the first character and lowercases all others # "This is a 'string'." #str.title() "this Is a 'String'".title() # "This Is A 'String'"
python list
Python
Copy
a=[0,1,2,3] remove by name a.remove(2) remove by indeox a.pop(1) insert by index a.insert(index,value)
List Operation on all element use map. python3 map return iterator
Python
Copy
x = '1 2 3 4 5' print(list(map(int, x.split(' '))))