如何在Python中將字串中的製表符展開為多個空格?
Python有效地管理字串之間的空格。在本文中,我們將研究在不確定需要多少空格時,如何在字串中新增空格的方法。
使用字串expandtabs()方法
Python中一個內建的方法叫做String expandtabs(),用於處理字串中的空格。Python中的expandtabs()方法建立一個字串的副本,其中所有制表符'\t'都替換為空格字元,直到下一個tabsize引數的倍數。
在某些情況下,可能需要定義在字串中應該留下多少空格,但具體數量取決於具體情況。
在這種情況下不斷調整字串是一個費力的過程。因此,Python庫中的“expandtabs()”方法定義了在替換“\t”符號之前應該向字串新增多少空格。
語法
以下是expandtabs()的語法:
string.expandtabs(size_of_tab)
其中,
size_of_tab_ 是替換\t的空格字元大小。預設為8。
返回值
返回的字串中,所有'\t'字元都將被空格字元替換,直到下一個製表符大小引數的倍數。
示例
沒有任何引數
在下面的示例中,我們沒有向expandtabs()函式傳遞任何引數:
string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs() print('The expanded string is:',result)
輸出
上面的例子中,Python字串expandtabs()函式沒有引數,即大小。因此,預設大小8被用來代替'\t'。
The expanded string is: Expanding tabs in string to multiple spaces.
示例
使用不同的引數
在下面的示例中,我們向expandtabs()函式傳遞不同的引數:
string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs(6) result1 = string.expandtabs(3) result2 = string.expandtabs(2) print('The expanded string is:',result) print('The expanded string is:',result1) print('The expanded string is:',result2)
輸出
在上面的程式碼示例中,Size = 6、Size = 3和Size = 2作為引數傳遞給Python字串expandtabs()函式。因此,大小分別為6、3和2個單位的空格被用來代替整個字串中的字元“\t”。
The expanded string is: Expanding tabs in string to multiple spaces. The expanded string is: Expanding tabs in string to multiple spaces. The expanded string is: Expanding tabs in string to multiple spaces.
expandtabs()中的錯誤和異常
如果我們嘗試傳遞一個不是整數型別的值(例如浮點數)作為引數,Python字串expandtabs()函式會丟擲一個TypeError異常。
因此,很明顯expandtabs()函式只接受整數型別的引數。以下是用非整數值作為引數傳遞expantabs()方法的示例:
示例
string = "Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces." result = string.expandtabs(6.2) print('The expanded string is:',result)
輸出
執行上述程式碼時會丟擲以下錯誤:
Traceback (most recent call last): File "main.py", line 2, in <module> result = string.expandtabs(6.2) TypeError: integer argument expected, got float
使用numpy.expandtabs()函式
Python Numpy模組中的numpy.char.expandtabs()函式執行與內建expandtabs()函式相同的函式。
如果使用者想要更精確,他們也可以將陣列作為引數傳遞給numpy.char.expandtabs()函式以及空格的大小。
語法
以下是numpy.expandtabs()的語法:
numpy.char.expandtabs(array,size_of_tab)
其中,
array 包含函式操作的元素。
size_of_tab(可選) 透過用這個可選引數替換製表符字元或“\t”,指定要提供的空格大小。
示例
以下是numpy.expantabs()函式的示例:
import numpy given_array = numpy.array("Expanding\ttabs\tin\tstring\tto\tmultiple\tspaces.") result = numpy.char.expandtabs(given_array,16) print('The expanded string array is:',result)
輸出
以下是上述程式碼的輸出:
The expanded string array is: Expanding tabs in string to multiple spaces.
使用replace()方法
Python中的字串類包含一個叫做replace的方法。它需要兩個字串作為輸入:一個要替換的字串和一個要用來代替它的字串。它與字串物件一起使用。
示例
以下是用replace()方法將字串中的製表符展開為多個空格的示例:
string = "Expanding tabs in string to multiple spaces." result = string.replace('\t', '') print( result)
輸出
以下是上述程式碼的輸出
Expanding tabs in string to multiple spaces.
使用re模組
Python的“re”模組也可以與正則表示式一起使用以達到相同的結果。要替換字串中的字元,請使用函式re.sub(要替換的正則表示式,要替換成的正則表示式,字串)。
示例
以下是用re模組將字串中的製表符展開為多個空格的示例:
import re string = "Expanding tabs in string to multiple spaces." result = re.sub('\t', '',string) print(result)
輸出
以下是上述程式碼的輸出:
Expanding tabs in string to multiple spaces.