python3 變量、字符串處理

弱語言:

所有的變量無需聲明即可使用;變量的數(shù)據(jù)類型可以隨時變更。

注釋

單行

/*
多行
*/

a=1
b=2
print(a+b)
print(type(a))
c=1.23
print(type(c))
d="hello"
print(type(d))

命名規(guī)則

標識符可以由字母、數(shù)字、下畫線()組成,其中數(shù)字不能打頭。
標識符不能是Python 關鍵字,但可以包含關鍵字。


image.png

標識符不能包含空格。

數(shù)值類型(空值None):整型 浮點型
整型(二進制、八進制、十進制、十六進制)
hex valuel = Ox13
hex value2 = OXaF
print ( ” hexValuel 的值為:”, hex_valuel)
print (” hexValue2 的值為:”, hex_value2)
#以Ob 或OB 開頭的整型數(shù)值是二進制形式的整數(shù)
bin val = Oblll
print ( ' bin_val 的值為: ', bin_ val)
bin val = OB101
print ( ’ bin_val 的值為:’, bin_val)
# 以Oo 或00 開頭的整型數(shù)值是八進制形式的整數(shù)
oct val = Oo54
print (’ oct_val 的值為:’, oct_val)
oct val= 0017
print ( ’ 。ct_val 的值為:’, oct_val)

浮點型
a=2.33

復數(shù):有個cmath模塊可以處理

字符串:可以用雙引號、單引號
str="sdfsdf"
print(type(str))
str="abc'ddd"
str="abc'ddd" ("\)
字符串拼接:
str1+str2

如果你要輸入一長串文字,用三引號

a="""
sdfdsfsd
sdfsdfds
sdfsdf
"""

字符串和整型互轉

str="123"
a=int(str)
b=str(a)
(float)

原始字符串

r ’ G: \publish\codes\02\2 . 4 ’
s2 = r ’”Let\’s q。”, sa 工d Charlie '

bool

True
False
"" None 都是False
and or not


image.png

簡便寫法:
st="a" if 1>2 else "bd"

bytes類型

b=bytes()
b2=b'xxx'
print(b3)
print (b3 [0])
pr 工nt(b3[2 : 4])
#調(diào)用bytes 方法將字符席轉換成bytes 對象
b4 =bytes (’我愛Python 編程’, enc。ding= ’ utf-8 ’)
bS = ”學習Pyth on 很有趣” .en code {『utf-8 ’)
print (bS)

解碼:decode

獲取用戶輸入

input("請輸入")
任何輸入都會被當作string

轉義字符

\n


image.png

字符串格式化,格式化輸出(需補充)

print (”the book ’ s price is %s ” %price)
print("aa{}{}".format(a,b))

字符串中的字符

image.png

image.png

image.png

內(nèi)置函數(shù):in len max min

image.png

查看類中的方法

dir(str)
help(str)

字符串中常用方法

str.lower()
str.upper()
str.title()

刪除空白

str.strip()
str.lstrip()
str.rstrip()

str.strip(".")
str.strip("abc")

startswith()
endswith()
find()

從索引8開始查詢

str.find("abc")
str.find("abc",8)
index()
replace("")

分割拼接

abc,def,ghi
s.split(",")
x.join(",")

運算符

      • /

位運算符

& | ^ ~ << >>

+=


image.png

索引

image.png

in

abc in s
abc not in s

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容