字符串元素《Python for Beginners》学习笔记(2) 字符串元素
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!《Python for Beginners》为LearnStreet上的Python入门课程。本节要主学习内容为符字串。
一、基本概念
1. Indexing - References a specific element of a sequence. The first element in a sequence is found at the index 0. To use a single element, call it with [], like this: variable_name.
首元素的引索值为0。
2. Negative Indexing - This allows you to reference values starting with the last element in a sequence. Count backwards from the end, starting with the index variable_name[-1].
正数引索值示表从末端元素开始从后向前数的引索值,末端元素从variable_name[-1]开始。
3. Slicing - Slicing allows you to use a particular subset of elements in a sequence. The format for slicing is variable_name , where it goes from the xth element and up to (but not including) the yth element.
variable_name示表从第x个元素到第y个元素(不含第y个元素)之间的元素序列。
4. Membership - uses the "in" operator to check if a string is found within a larger string.
用使in操作符检测一个符字串是不是属于另一个大符字串。
5. A function is basically a way to give several lines of code to one variable name. When the function is called, all of the code in the function is run.
函数即将多行代码付与一个量变名,调用该函数就行执函数中的代码。
二、编程训练
1. 符字串合并(串联)
>teach = "Learn"
>teach = teach + "Street"
>teach
=>LearnStreet
(注:=>前面为出输结果)
2. 负引索值
>py = "python"
>py[-2]
=> 'o'
3. 符字串片切
>w = "watermelon"
>w
=> 'water'
4. 符字(串)检索
>"i" in "team"
=> False
5.用使len()函数算计符字串长度
>len("hello")
=> 5
>len(phrase)
=> 184
(注:phrase为已明声过的符字串量变)
6.小验测
在符字串量变book所包括的符字串的前半分部中检索是不是含有"peanut"符字串。
>"peanut" in book
=> True
(本文完)
文章结束给大家分享下程序员的一些笑话语录: Bphone之你们聊,我先走了!移动说:我在phone前加o,我叫o缝;苹果说:我在phone前i,我是i缝;微软说:我在phone前加w,我叫w缝;三星说:你们聊,我先走了!
将来王建宙写回忆录的时候,一定要有一句“常小兵为中国移动的发展做出了不可磨灭的贡献”。
页:
[1]