字串内顯示單引號’的方法
#外面用""包住
>>>print "I'm Python"
"I'm Python"
字串内顯示雙引號"的方法
#外面用'包住
>>>print 'this is a "quote"'
'this is a "quote"'
換行符號:\n
>>>print "Here is new line \n and here is second line"
"Here is new line
and here is second line"
tab縮排符號:\t
>>>print "Here is new line \t and here is second line"
"Here is new line and here is second line"
Python 3 :print 視為函數(),非述句
#匯入P3 Module
>>>from __future__ import print_function
>>>print("Hello World")
"Hello World"
len(),計算字元數
#len裡面要放字串形式
#空白也會計算進去
>>>len("Hello World")
11
indexing
#先把字串存成變數
>>>s="Hello World"
#使用[],找對應的字元
>>>s[0]
"H"
#從0開始算
#空格也會算進去
#找某個字以後的東西
#1代表這個字e (H是0)
#冒號: 代表~
>>>s[1:]
"ello World"
#找某個字以前的東西
#3代表這個字l (H是0)
#冒號: 代表~
>>>s[:3]
"Hel"
#找所有字元[:]
>>>s[:]
"Hello World"
#[負數] 從末端找回來
#H是0,-1會回到末端找到d
>>>s[-1]
"d"
#找除了最後一個字以外的
>>>s[:-1]
"Hello Worl"
#:: 表示從頭到尾
#[::1] 表示1次踏1階(每個字都顯示)
>>>s[::1]
"Hello World"
#[::2] 表示1次踏2階(每兩個字顯示一個字)
>>>s[::2]
"HloWrd"
#[::-1]反轉,倒退走
>>>s[::-1]
"dlroW olleH"
immutability 字串的不可修改性
>>>s="Hello World"
"Hello World"
>>>s[0]="X"
不可以改字串裡面的內容
#但是可以增加
>>>s + "concatenate me"
"Hello World concatenate me"
upper() 改大寫 / lower() 改小寫
>>>s="Hello World"
"Hello World"
>>>s.upper()
"HELLO WORLD"
>>>s.lower()
"hello world"
split() 切割
>>>s="Hello World"
Hello World
>>>s.split("e")
["H", "llo World"]