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)
时间: 2024-10-28 14:50:57

Learn Python 004: string slicing的相关文章

python中string模块各属性以及函数的用法

任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 字符串属性函数 系统版本:CentOS release 6.2 (Final)2.6.32

How to learn Python

https://www.udemy.com/python-programming-for-real-life-networking-use/ https://pynet.twb-tech.com/blog/python/books-beginners.html Python Book Recommendations for Beginners (2014-01-17) By Kirk Byers You have decided to learn Python, but which Python

Python学习--字符串slicing

Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages Python indexes and slices for a six-element list. Indexes enumerate the elements, slices enumerate the spaces between the elements. Index from rear: -6 -5 -4 -3 -2

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

最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 3: 数字和数学计算 习题 4: 变量(variable)和命名 习题 5: 更多的变量和打印 习题 6: 字符串(string)和文本 习题 7: 更多打印 习题 8: 打印,打印 习题 9: 打印,打印,打印 习题 10: 那是什么? 习题 11: 提问 习题 12: 提示别人

[IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果你觉得英文版看着累,当当网有中文版,也有电子版可以选择. 我试着将其中的代码更新到Python 3.同时附上一些自己的初学体会,希望会对你有帮助. 中文版有人把书名翻译为<笨办法学python>,其实我觉得叫做<学Python,不走寻常路>更有意思些. 作者的意思你可以在序言中详细了解

Learn Python the Hard Way--Exercise 46

0. 缘起 <Learn Python the Hard Way>Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use th

Learn Python From &#39;Head First Python&#39; [3](2) : Pickle

1.the use of 'with open... as ...' 2.the use of pickle(dump and load) for Step1: the 'with open ... as...' is the short format of 'try...except...finally' for Step2: you can store a list with pickle.dump() and get the content again with pickle.load()

Learn Python From &#39;Head First Python&#39; [2] : Sharing

1.publish 2.update print_lol PS: for Step 1. write the code in to a py file. put the py file into a folder named nester. new a py file named 'setup' and the content as below: setup( name = 'nester', version = '1.0.0', py_modules = ['nester'], author

Learn Python From &#39;Head First Python&#39; [1] : The List

1.the concept of List 2. how to define a method of myself 3.how to iterator a list PS: for Step 2,3. it is related with a key word: 'def'. we define a function like below. def function name: XXXXX e.g. list iterator : 1 def print_lol(the_list): 2 for