python字串和字元

2021-10-03 03:24:41 字數 1381 閱讀 1997

python沒有字元資料型別,只有字串。

字串必須在 』 』 (單引號) " 「(雙引號) 裡面 『hello world』 == 「hello world」(為了和其他語言保持一致通常把』 '用於括住單個字元,」 「用於括住字串)

『』』 『』』 == 「」」 「」" (三引號),三引號中可以包含換行符

2.ascii碼和統一碼 ascll碼屬於統一碼

ascii碼 二進位制0000 0000 ~ 0111 1111 128個

ord(『a』) 返回字元』a』所對應的ascll碼

chr(97) 返回數字97所對應的字元

統一碼 十六進製制\u0000 ~ \uffff 65536個所以只能表示部分漢字

3.轉義字元 —使用

print

("joe said,\"he is boring\" "

)

import os

os.system(

"note"

+"pad"

+' '

+"c:\\users\\pandora\\desktop\\"

+str(2

))

4.字串連線

str1 =

"note"

"pad"

或者

str1 =

"note"

+"pad"

字串變數之間連線用 +

5.從控制台讀取字元 input

str = input(「please input your str」)

*> c語言從控制台讀取字元

①int getchar(void) 從輸入的字串中讀取第乙個字元,回顯②int getch()讀取第乙個輸入的字元結束函式,不回顯

讀取字串

①char * gets(char* str)

char str

[100];

printf(

"enter a value :");

gets(

str);``

> ②int scanf(const char *

format,.

..)>

```python

char str

[100];

int i;

printf(

"enter a value :");

scanf(

"%s %d"

,str

,&i)

;

python字串 Python 字串

建立字串很簡單,只要為變數分配乙個值即可。例如 var1 hello world var2 python runoob python訪問字串中的值python不支援單字元型別,單字元在 python 中也是作為乙個字串使用。python訪問子字串,可以使用方括號來擷取字串,如下例項 例項 pytho...

python字串 python字串

單引號示例常用的轉義字元 轉義字元案例1format 格式化練習1 help sisdigit,isnumeric,isdecimal 三個判斷數字的函式 isalnum 檢查字串是否由字母加數字組成 s test1split 字串拆分 splitlines 已換行符拆分 join 合成字串 upp...

python 字串和轉義字元

字串是一種表示文字的資料型別,字串可以使ascii字元 各種符號以及各種unicode字元,在python中,共有三種字串的表現形式 雖然三種方式的最終含義是一致的,但是當字串中包含 時不可以使用單引號 同理當字串中包含 時不可以使用雙引號。單引號 a pytonn print a 雙引號 b py...