Built-in String Methods

2017-07-17  本文已影响0人  cutelittlePanda

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
上一篇下一篇

猜你喜欢

热点阅读