In [ ]:
ord('T'), chr(84)
In [ ]:
'' in 'A string', 'str' in 'A string', 'St' not in 'A string'
In [ ]:
print(len('Is this REALLY worth $38.20?'))
print('Is this REALLY worth $38.20?'[8])
print('Is this REALLY worth $38.20?'[8: 15])
print('Is this REALLY worth $38.20?'[8: 15: 2])
print('Is this' + ' REALLY worth ' + '$38.20?')
print('Is this' ' REALLY worth '
'$38.20?')
print('$38.20' * 3)
In [ ]:
S = 'Is this REALLY worth $38.20?'
print(S.casefold())
print(S.lower())
print(S.capitalize())
print(S.title())
print(S.upper())
print(S.swapcase())
In [ ]:
S = 'Is this REALLY worth $38.20?'
# Next six strings have a width of 50
print(S.center(50))
print(S.center(50, '_'))
print(S.ljust(50))
print(S.ljust(50, '_'))
print(S.rjust(50))
print(S.rjust(50, '_'))
print()
print('-123.45'.zfill(4))
print('-123.45'.zfill(12))
print()
print('Use \t to insert a tab.')
# The default, which can be changed, is to replace a tab by 8 spaces
print('Use \t to insert a tab.'.expandtabs())
print('Use \t to insert a tab.'.expandtabs(12))
In [ ]:
print(' \n\t A string with leading and trailing spaces \t\n '.strip())
print(' \n\t A string with leading and trailing spaces \t\n '.lstrip())
print('***')
print(' \n\t A string with leading and trailing spaces \t\n '.rstrip())
print('Another string without leading and trailing spaces'.strip('eAconsiw'))
print('Another string without leading and trailing spaces'.lstrip('eAconsiw'))
print('Another string without leading and trailing spaces'.rstrip('eAconsiw'))
In [ ]:
print('Forêt'.isalpha())
print('größer_123'.islower())
print('ΕΛΛΑΣ$><$!'.isupper())
print(' String Reference 1345 Mrfc…'.istitle())
print('0123'.isdecimal())
print('0123'.isdigit())
print('\u00B2 is also a digit')
print('0123'.isnumeric())
print('\u00BD is also numeric')
print('012AbC'.isalnum())
print('No_digit_such_as_8_in_a_Python_identifier'.isidentifier())
print(' \t\n\f\r'.isspace())
print('˜µ≥!a9溧…≈≥!'.isprintable())
In [ ]:
print('baabababababaab'.count('aba'))
print('baabababababaab'.count('aba', 3))
print('baabababababaab'.count('aba', 3, 11))
print('baabababababaab'.count('aba', 3, 10))
print()
print('baabababababaab'.find('aba'))
print('baabababababaab'.find('aba', 3))
print('baabababababaab'.find('aba', 3, 7))
print('baabababababaab'.find('aba', 3, 6))
print('baabababababaab'.index('aba'))
print('baabababababaab'.index('aba', 3))
print('baabababababaab'.index('aba', 3, 7))
print()
print('baabababababaab'.rfind('aba'))
print('baabababababaab'.rfind('aba', -4))
print('baabababababaab'.rfind('aba', 1, 8))
print('baabababababaab'.rindex('aba'))
print('baabababababaab'.rindex('aba', 1, 8))
print()
print('baabababababaab'.startswith('baab'))
print('baabababababaab'.endswith('abaab'))
In [ ]:
print('baabababababaab'.replace('aba', '_NEW_'))
print('baabababababaab'.replace('aba', '_NEW_', 2))
print()
# 99 is the Unicode ordinal of character c
print('abcdefgabcdefg'.translate(str.maketrans({'a': '_A_', 99: '_C_', 'd': None })))
print('abcdefgabcdefg'.translate(str.maketrans('acd', 'ACD')))
print('abcdefgabcdefg'.translate(str.maketrans('acd', 'ACD', 'ef')))
In [ ]:
print('abc;defg;,hijkl;,mnop'.partition(';,'))
print('abc;defg;,hijkl;,mnop'.partition(',;'))
print()
print('abc;defg;,hijkl;,mnop'.rpartition(';,'))
print('abc;defg;,hijkl;,mnop'.rpartition(',;'))
print()
print('abc;defg;,hijkl;,mnop'.split(';,'))
print('abc;defg;,hijkl;,mnop'.split(';,', 1))
print(' \n\t abc \n\t defg \n\t hijkl \n\t mnop \n\t '.split())
print(' \n\t abc \n\t defg \n\t hijkl \n\t mnop \n\t '.split(maxsplit = 1))
print()
print('abc;defg;,hijkl;,mnop'.rsplit(';,'))
print('abc;defg;,hijkl;,mnop'.rsplit(';,', 1))
print(' \n\t abc \n\t defg \n\t hijkl \n\t mnop \n\t '.rsplit())
print(' \n\t abc \n\t defg \n\t hijkl \n\t mnop \n\t '.rsplit(maxsplit = 1))
print()
print('''adc
defg
hijkl
mnop'''.splitlines())
print('''adc
defg
hijkl
mnop'''.splitlines(True))
In [ ]:
print('_*_'.join(['a', 'bc', 'de', 'f']))
print('_*_'.join(('a', 'bc', 'de', 'f')))
print('_*_'.join({'a', 'bc', 'de', 'f'}))
print('_*_'.join({'a' : 1, 'bc' : 2, 'de': 3, 'f': 4}))