填充字串到指定長度

2021-10-05 11:56:23 字數 925 閱讀 9000

填充字串到指定長度

import sys

defpadding_str

(txt, widt_h, lef_t, righ_t)

:if lef_t ==

'true'

and righ_t ==

'false'

:return

'}'.

format

(txt, widt_h)

# 1 字元在左邊,*在右邊填充

elif lef_t ==

'false'

and righ_t ==

'true'

:return

'}'.

format

(txt, widt_h)

# 2 字元在友邊,*在左邊填充

elif lef_t ==

'true'

and righ_t ==

'true'

:return

'}'.

format

(txt, widt_h)

# 3 字元在中間,*在兩邊邊填充

for line in sys.stdin:

widt_h =

int(

input

("請輸入字串長度:"))

lef_t =

str(

input

("請輸入true或者false確認是否在左邊填充:"))

righ_t =

str(

input

("請輸入true或者false確認是否在左邊填充:"))

print

(padding_str(line.strip(

), widt_h, lef_t, righ_t)

)# 1

將字串不足長度的填充到指定長度

str pad 使用另乙個字串填充字串為指定長度 使用 stringstr pad string input,int pad length string pad string int pad type str pad right 說明 該函式返回input被從左端 右端或者同時兩端被填充到制定長度後...

按照指定長度切割字串

按照指定長度分割字串 param inputstring 需要切割的源字串 param length 指定的長度 return public static string getdivlines string inputstring,int length if remainder 0 string s...

printf 列印 指定長度 字串

原樣輸出字串 printf s str 2.輸出指定長度的字串,超長時不截斷,不足時右對齊 printf ns str n 為指定長度的10進製數值 3.輸出指定長度的字串,超長時不截斷,不足時左對齊 printf ns str n 為指定長度的10進製數值 4.輸出指定長度的字串,超長時截斷,不足...