Learn Python 004: string slicing

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

learn python the hard way 习题6~10总结

004 String 是最基本的数据类型吗?

笨办法学 Python (Learn Python The Hard Way)

learn the python the hard way习题11~17总结

python_004days

Python初级004-字典