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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容