Python基礎 String字串

2021-07-22 16:24:05 字數 1052 閱讀 1118

#!usr/bin/env/ python

#coding:utf-8

#1.單雙引號轉義及字串拼接、前台介面輸入

print('hello')

print("word")

print('hello'' word!')

print('hello'+' word')

print('hel\"lo')

'''name = input('please input:')

print(name+'!')

'''#2.字串格式化d/f/s

pi = 3.1415926

str = 'wangwang'

print('pi=%d'%pi)

print('pi=%5.2f'%pi)

print('string=%s'%str)

#3.字串方法(find/split/join/strip/lower/replace)

'''find:在乙個較長的字串中查詢子串,他返回子串所在位置最左端索引,如果沒有則返回-1

split:將字串按照一定規則分割成乙個列表序列

join:將乙個序列按照某種規則拼接成字串

strip:取出字串兩側空格

lower:返回字串小寫字母版

replace:替換字串

'''title = ' what the **** off! '

print(title.find('the'))

print(title.find('tthe'))

print(title.split(' '))

list1 = title.split(' ')

sep = '+'

print(sep.join(list1))

print(title)

print(title.strip())

print(title.lower())

print(title.replace('the','is'))

dirs = '','usr','bin'

print('/'.join(dirs))

python的基礎字典 Python 基礎 字典

每個鍵是從它的值由冒號 即在專案之間用逗號隔開,整個東西是包含在大括號中。沒有任何專案乙個空字典只寫兩個大括號,就像這樣 鍵在乙個字典中是唯一的,而值則可以重複。字典的值可以是任何型別,但鍵必須是不可變的資料的型別,例如 字串,數字或元組這樣的型別。訪問字典中的值 要訪問字典元素,你可以使用方括號和...

python 類 字典 python基礎型別 字典

字典 字典是python中唯一的對映型別,採用鍵值對 key value 的形式儲存資料。python對key進行雜湊函式運算,根據計算的結果決定value的儲存位址,所以字典是無序儲存的,且key必須是可雜湊的。可雜湊表示key必須是不可變型別,如 數字 字串 元組。字典 dictionary 是...

String擴充套件Extension 去除字串空格

文字列 初期化 let str1 最初 最後 除 let str2 str1.trimmingcharacters in whitespaces characterset 裡各個列舉型別的含義如下 屬性描述 alphanumerics 字母和數字的組合,包含大小寫,不包含小數點 capitalize...