首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。