Python基本資料型別之字串str

2022-09-25 09:12:12 字數 3088 閱讀 1492

print("hello world")

print('hello world')

print("""hello world""")

# 輸出結果

hello world

h程式設計客棧ello world

hello world

為什麼需要單引號,又需要雙引號

因為可以在單引號中包含雙引號,或者在雙引號中包含單引號

# 單雙引號

print("hello 'poloyy' world")

print('this is my name "poloyy"')

# 輸出結果

hello 'poloyy' world

this is my name "poloyy"

正常情況下,單引號和雙引號的字串是不支援直接在符號間換行輸入的,如果有需要可以用多引號哦!

# 多行字串

print("""

hwww.cppcns.comello

wuagqobwhorld

""")

print("""

this

ismy

name

poloyy

""")

# 輸出結果

hello

world

this

ismy

name

poloyy

在字元前加 \ 就行

常見的有

栗子比如在字串雙引號間還有乙個雙引號,就需要用轉義符

# 轉義符

print("hello \"poloyy\" world")

print('my name is \'poloyy\'')

# 輸出結果

hello "poloyy" world

my name is 'poloyy'

假設 \ 只想當普通字元處理呢?

print("反斜槓 \\ 是什麼")

print("換行符是什麼 \\n")

# 輸出結果

反斜槓 \ 是什麼

換行符是什麼 \n

window 路徑的栗子

print("c:\nothing\rtype")

print("c:\\nothing\\rtype")

# 輸出結果

c:\nothing\

c:type

c:\nothing\rtype

更簡潔的解決方法

用轉義符會導致可讀性、維護性變差,python 提供了乙個更好的解決方法:在字串前加r

print(r"c:\nothing\rtype")

# 輸出結果

c:\nothing\rtype

python3的url編碼和解碼,自定義gbk、utf-8的例子

獲取字串中某個字元

字串是乙個序列,所以可以通過下標來獲取某個字元

# 獲取字串某個字元

str = "hello world"

print(str[0])

print(str[1])

print(str[6])

print(str[-1])

print(str[-5])

# 輸出結果hewdl

如果是負數,那麼是倒數,比如 -1 就是倒數第乙個元素,-5 就是倒數第五個元素

python 中,可以直接通過切片的方式取一段字元

切片的語法格式

str[start : end : step]

栗子print("hello world'[:] ", 'hello world'[:]) # 取全部字元

print("hello world'[0:] ", 'hello world'[0:]) # 取全部字元

print("hello world'[6:] ", 'hello world'[6:]) # 取第 7 個字元到最後乙個字元

print("hello world'[-5:] ", 'hello world'[-5:]) # 取倒數第 5 個字元到最後乙個字元

print("hello world'[0:5] ", 'hello world'[0:5]) # 取第 1 個字元到第 5 個字元

print("hello world'[0:-5] ", 'hello world'[0:-5]) # 取第 1 個字元直到倒數第 6 個字元

print("hello world'[6:10] ", 'hello world'[6:10]) # 取第 7 個字元到第 10 個字元

print("hello world'[6:-1] ", 'hello world'[6:-1]) # 取第 7 個字元到倒數第 2 個字元

print("hello world'[-5:-1] ", 'hello world'[-5:-1]) # 取倒數第 5 個字元到倒數第 2 個字元

print("huagqobwhello world'[::-1] ", 'hello world'[::-1]) # 倒序取所有字元

print("hello world'[::2] ", 'hello world'[::2]) # 步長=2,每兩個字元取一次

print("hello world'[1:7:2] ", 'hello world'[1:7:2]) # 步長=2,取第 2 個字元到第 7 個字元,每兩個字元取一次

# 輸出結果

hello world'[:] hello world

hello world'[0:] hello world

hello world'[6:] world

hello world'[-5:] world

hello world'[0:5] hello

hello world'[0:-5] hello

hello world'[6:10] worl

hello world'[6:-1] worl

hello world'[-5:-1] worl程式設計客棧

hello world'[::-1] dlrow olleh

hello world'[::2] hlowrd

hello world'[1:7:2] el

python 提供了很多內建的字串函式,具體可看

python之基本資料型別

1 數字型別 整形 int 1.1 作用 記錄年齡 身份證號 個數等 1.2 定義 age 18print type age 浮點型 float 1.3 作用 記錄薪資 身高 體重 1.4 定義 salary 3.3height 1.87 weight 70.3 print type salary ...

Python入門之基本資料型別

由3部分組成 python 裡面一切都是指標 型別也分為可變型別和不可變型別 其中可變型別 int string,tuple a 1,2,3 可變型別如list,tuple,dict 注意 python雖然是動態語言,但是如果變數的型別確定了之後,就不能更改。整型與boolean print 1 1...

Python入門之基本資料型別

1.整數型別 可正可負,沒有範圍。4種進製表示形式 十進位制 1008,101 二進位制 0b1001,0b1001 八進位制 0o123,0o456 十六進製制 0x789,0xabc 2.浮點數 運算存在不確定尾數 如 0.1 0.2 0.3 round 0.1 0.2,1 0.3 round ...