首页 > 代码库 > Learn Python 004: string slicing

Learn Python 004: string slicing

monthsofyear = ‘JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember‘
monthof1 = monthsofyear[0:7:1]
print(monthof1)
monthof2 = monthsofyear[7:15:1]
print(monthof2)
monthof3 = monthsofyear[15:20:1]
print(monthof3)
gibberish1 = monthsofyear[::2]
print(gibberish1)
gibberish2 = monthsofyear[7::2]
print(gibberish2)
gibberish3 = monthsofyear[::-1]
print(gibberish3)
monthof4 = monthsofyear[monthsofyear.index(‘June‘):monthsofyear.index(‘July‘)]
print(monthof4)
monthof5 = monthsofyear[monthsofyear.index(‘Jul‘):monthsofyear.index(‘Aug‘)]
print(monthof5)
monthof11 = monthsofyear[monthsofyear.index(‘No‘):monthsofyear.index(‘De‘)]
print(monthof11)
monthof12 = monthsofyear[monthsofyear.index(‘De‘):]
print(monthof12)

# email slicer
# step 1: get user email address
email = input(‘Please enter your email address: ‘).strip()
# step 2: slice out user name
username = email[:email.index(‘@‘)]
# print(username)
# step 3: slice domain name
domain = email[email.index(‘@‘) + 1 :]
# print(domain)
# step 4: format message
output = ‘Your username is "{}" and your domain name is "{}".‘.format(username, domain)
# step 5: display output message
print(output)

 

Learn Python 004: string slicing