Built-in String Methods

str.isalpha() : checks whether the string consists of alphabetic characters only.

str.isdigit()?: checks whether the string consists of digits only.

str.islower():?

str.isupper()

str.center(width, fillchar) : returns centered in a string of length width. Padding is done using the specified fillchar. Default filler is a space.

def main():

s = input('Please enter a digit: ')

l = [int(s), int(s.center(2,s)), int(s.center(3,s)), int(s.center(4,s))]

total = sum(l)

print(int(s))

print(l)

print(total)

Result:

=============================exercise15.py =============================

Please enter a digit: 9

9

[9, 99, 999, 9999]

11106

>>>

=============================exercise15.py =============================

Please enter a digit: 1

1

[1, 11, 111, 1111]

1234

>>>

str.startswith(): checks whether the string starts with str, optionally restricting the matching with the given indices start and end.

Parameters:

str? This is the string to be checked.

beg? This is the optional parameter to set start index of the matching boundary.

end? This is the optional parameter to set start index of the matching boundary.

Return Value

This method returns true if found matching string otherwise false.

sample code;

'delimiter'.join(seq): The?join()method returns a string in which the string elements of sequence have been joined by str separator/delimiter.

>>> delimiter=''

>>> seq=['list1']

>>> seq2=['list2']

>>> delimiter.join(seq+seq2)

'list1list2'

>>> seq+seq2

['list1', 'list2']

Slicing and Reverse of string:

Slicing and Reversing
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容