1. 程式人生 > >python筆記一

python筆記一

使用 特殊字符 換行符 lac 對象 please hello 查看 表示

字符串學習

---------- input交互輸入----------

name = input( ‘please enter your name:‘ )
print (name)

---------- input 交互輸入計算----------

value1 = 1024 * 512
value2 = input(‘num1:‘)
value3 = input(‘num2:‘)
print (value1)
print (value2)
print (value3)
print (value1 * value2)
print (value1 * value3)
#當value2 * value3的時候不能如上表示

---------- in 判斷變量/值中包含的字符串----------

s = "Wind and cloud"
weather1 = "Wind" in s
weather2 = "SB" in s
print (weather1)
print (weather2)

---------- 變量元組 ----------

Wind = "weather1"
Paul = "name"
Rain = "weather2"
_Coll_1 = [‘Wind‘,‘Paul‘,‘Rain‘]
print(_Coll_1)
_Cont_1 = "Wind" in _Coll_1
print (_Cont_1)

---------- type 查看對象的類,一般字符串都是標準輸入----------

temp1 = "123"
_123_io = type(temp1)
print(_123_io)
temp2 = "hey"
_hey_io = type(temp2)
print(_hey_io)

---------- dir 查看功能----------

temp = "wind"
print(dir(temp))

---------- upper 小寫轉換成大寫----------

temp = "wind"
print(temp.upper())

---------- count 字符串統計一定要在一對引號之內的字符串----------

_name_coll = ("paul,vencent,sunny,anny,should,bilibili,david,paul")
_coll = _name_coll.count("nn")
print(_coll)

---------- endswith 字符串尾部輸出定義----------

temp = "hello"
print(temp.endswith(‘e‘,0,2))    
#字符串的0-2的字符he,是以e結尾,0也可以寫成1

---------- center 占位輸出----------

a1 = "alex"
a2 = a1.center(20,‘*‘)
print(a2)
#字符總數20個,以alex為中心,兩邊以*號填充

---------- count 統計指定字符串數量----------

a1 = "alex is alpa"
a2 = a1.count(‘al‘,5,12)
print(a2)

---------- expandtabs 分隔符設計20位----------

content = "hello\t123"
print(content)
print(content.expandtabs())
print(content.expandtabs(20))

---------- find 字符串位數查找

s = "alex hello"
print(s.find("h"))
print(s.find("p"))
#找不到的字符,會顯示負數

---------- str 字符串標準輸出,字符串位置輸出定義

str = ‘Runoob‘
print  (str[0:3])
print (str[0])
print (str[2:5])
print (str[2:])
print (str * 2)
print (str + "TEST")

---------- \n 本身是換行符,r將字符串中特殊字符轉化為普通字符

print(‘Ru\nnoob‘)
print(r‘Ru\nnoob‘)

---------- format 占位替換,有序排列

x = "hello {0}, age {1}"
print(x)

y = x.format("paul","27")
print(y)

---------- join

alex = "name1"
should = "name2"
paul = "name3"
dio = "name5"
name = [alex ,should ,paul ,dio]
_name_join = "".join(name)
print(_name_join)
#將值變成一個整體字符串輸出,如果不用元組,只是字符串,可以使用雙引號把字符串引起來,加入空格,是字符串相互隔開。
name = "alex ","should ","paul ","dio "
print (name)
_name_join = "".join(name)
print(_name_join)

---------- replace 替換字符串

x = "alex is alex"
y = x.replace("al","AA")
print(y)

---------- split 對字符串進行切片分隔

x = "alex\nalex"
y = x.split()
print(y)

python筆記一