Python3筆記(1) 字串去除空格的方法小結

2021-08-29 04:37:11 字數 1404 閱讀 3856

def get_variable_attribute(self, path_map_file):

attribute_list =

file_map = open(file=path_map_file, mode="r", encoding="utf-8")

map_lines_list = file_map.readlines()

file_map.close()

for line in map_lines_list:

if self.name in line:

# other method for variable address

# self.start_address = line[0:10]

# self.end_address = line[11:21]

new_line = line.split(" ", 10)

for str in new_line:

if str != "":

self.start_address = attribute_list[0].upper()

self.end_address = attribute_list[1].upper()

self.size = attribute_list[2]

break

>>> a = " a b c "

>>> a.strip()

'a b c'

>>> a = " a b c "

>>> a.lstrip()

'a b c '

>>> a = " a b c "

>>> a.rstrip()

' a b c'

# replace主要用於字串的替換replace(old, new, count)

>>> a = " a b c "

>>> a.replace(" ", "")

'abc'

# join為字元字串合成傳入乙個字串列表,split用於字串分割可以按規則進行分割

>>> a = " a b c "

>>> b = a.split()  # 字串按空格分割成列表

>>> b ['a', 'b', 'c']

>>> c = "".join(b) # 使用乙個空字串合成列表內容生成新的字串

>>> c 'abc'

# 快捷用法

>>> a = " a b c "

>>> "".join(a.split())

'abc'

python3字串相等 python3 字串

1 拼接 1 多個字串進行連線 連線符,必須左右資料型別一致 例 print hello world 結果 helloworld 例 print 5 world 結果 typeerror unsupported operand type s for int and str 2 多個相同字串連線 字串...

python3 字串基礎

字串可以使用一對單引號或一對雙引號指定起止位置,兩種方式指定的字串完全等價。如 hello 和 world 可以用三引號 或 指定多行字串,其中可自由使用單 雙引號而不需轉義。如 what s your name?i asked.字串過長不方便寫在一行時,可以使用反斜槓跨行而不增加換行符。如 abc...

python3 字串操作

auther aaron fan name my tname is age is print name.capitalize 這段話的首字母大寫 print name.count a 統計這段字串中一共有多少個a print name.casefold print name.center 50,一共...