字符串操作

字符串是语言中使用最多的,下面我们来看看python为字符串提供哪些方法:

1、upper()、lower()、title() 这3个方法都是返回一个新的字符串。重要性:**

name = 'example_EXAMPLE'

print(name.upper())  # 转换为大写
print(name.lower()) # 转换为小写
print(name.title()) # 首字母大写 # 输出 EXAMPLE_EXAMPLE
example_example
Example_Example

2、isdigit()、isalnum()、isspace()、isappha() 返回bool类型。重要性:***

name = 'example_EXAMPLE'
print(name.isdigit()) # 是否是数字
print(name.isalnum()) # 是否是字母和数字
print(name.isspace()) # 是否是空格
print(name.isalpha()) # 是否是字母 # 输出 False
False
False
False

3、startswith()、endswith返回bool类型,判断字符串开始或结束是否为真。重要性:****

name = 'example_EXAMPLE'
print(name.startswith('ex'))
print(name.endswith('LE')) # 输出
True
True

4、split() 这个方法非常重要,它会根据字符串里面制定的字符进行分割,返回列表。 重要性:*****

name = 'tom&jerry'
name_list = name.split('&')
print(name_list[0])
print(name_list[1]) # 输出
tom
jerry

5、join()的方法刚好跟split相反,它会把列表转换成字符串。重要性:*****

fruit_list = ['apple', 'banana', 'pear', 'orange']
new_val = '&'.join(fruit_list)
print(new_val) # 输出
apple&banana&pear&orange

6、strip()、lstrip()、rstrip() 去空格。重要性:**

example = '  abc   '  # len = 8
print(example.strip()) # 去掉左右空格
print(len(example.strip())) #
print(example.lstrip()) # 去左边空格
print(len(example.lstrip())) #
print(example.rstrip()) # 去右边空格
print(len(example.rstrip())) #

7、rjust()、ljust()、center() 重要性:**

example = 'abc'
print(example.rjust(20, '*'))
print(example.ljust(20, '#'))
print(example.center(20, '=')) # 输出
*****************abc
abc#################
========abc=========

While循环

while语句是python语言中第二种循环,它和for循环在功能是都是一致的,不同点在于,for循环在迭代完毕后停止,而while循环是在条件不成立的时候停止。有的时候我们可能会遇上一种死循环,例如:

count = 0
while True:
print('hello world ', count)
count += 1

这个语句就是因为while后边的条件为真,所以会一直运行下去,那我们改改,让它的条件在到达5的时候程序停止跳出循环。

count = 0
while count < 5:
print('hello world ', count)
count += 1

其实让while停下来有很多种方式,下面我介绍一个break,这个例子就是让用户输入3次,如果不正确就退出并打印,如果正确就输出ok

count = 0
while count < 3:
inp = input('input your name :') if inp == 'chen':
print('ok')
break
count += 1
else:
print('More times')

通过上边的例子我们稍微修改一下,引入continue看看有什么变化,当用户输入的值为空的时候我们要求他必须输入值,continue会跳出当次循环。

count = 0
while count < 3:
inp = input('input your name :')
if len(inp) == 0:
continue
if inp == 'chen':
print('ok')
break
count += 1
else:
print('More times')

列表和元祖

列表和元祖可以使编程在处理大量数据的时候变得更加容易和灵活,而且,列表和元祖自身可以嵌套,在python中列表以方括号[],元祖是以小括号()来进行放置数据,看起来像这样['apple','banana','pear'],列表或元祖中的值也可以叫元素。它们可以包含多个值,也可以不包含值,例如[]或(),这代表是一个空列表或空元祖,下面我们对是用列表很元祖的一些方法进行举例:

1、用下标进行取值: 此方式适合于列表和元祖

下图展示了一个水果列表,在该列表中有4个值,我们可以通过下标进行取值。列表下标的值是从0可以,所以如果你要取出apple,就需要是用下边0,如fruit_list[0],其余以此类推。

fruit_list = ['apple', 'banana', 'pear', 'orange']
print(fruit_list[0])
print(fruit_list[1])
print(fruit_list[2])
print(fruit_list[3])

注意:

下边只能是整数,不能写成浮点类型

2、负数下标:此方式适合于列表和元祖

虽然下标是从0开始并向上增长,但是有的时候为了方便取值,我们也可以通过负数进行取值,比如

fruit_list = ['apple', 'banana', 'pear', 'orange']

print(fruit_list[-1])
print(fruit_list[-2]) # 输出
orange
pear

3、切片取值:此方式适合于列表和元祖

我们知道字符串可以通过切片进行取值,列表跟字符串取值完全一样,比如

fruit_list = ['apple', 'banana', 'pear', 'orange']
print(fruit_list[:]) # 打印全部的值
print(fruit_list[-1]) # 打印最后一个值
print(fruit_list[1:]) # 打印下标从1到最后的值
print(fruit_list[0:3]) # 打印从apple到pear的值
print(fruit_list[:-1]) # 打印从apple到pear的值
print(fruit_list[::2]) # 打印全部的值,步长为2 # 输出
['apple', 'banana', 'pear', 'orange']
orange
['banana', 'pear', 'orange']
['apple', 'banana', 'pear']
['apple', 'banana', 'pear']
['apple', 'pear']

4、修改列表里面的值:元祖不能是用

fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list[1] = 'grape' # 修改banana为grape
print(fruit_list) # 输出
['apple', 'grape', 'pear', 'orange']

5、插入值:元祖不能是用

要在列表中添加新值,有2种方式,一个是insert,一个是append

append是插入列表最后

fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.append('grape')
print(fruit_list) #输出
['apple', 'banana', 'pear', 'orange', 'grape']

insert是插入到下标制定某个地方,比如我要把grape插入到apple后面

fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.insert(1, 'grape')
print(fruit_list) # 输出
['apple', 'grape', 'banana', 'pear', 'orange']

6、删除:元祖不能是用

删除有3种方法,del,pop,remove

del不是列表独有的方法,可以在处理字符串种是用

fruit_list = ['apple', 'banana', 'pear', 'orange']
del fruit_list[0] # 删除apple
print(fruit_list) # 输出
['banana', 'pear', 'orange']

remove

fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.remove('apple') # 删除apple
print(fruit_list)
# 输出
['banana', 'pear', 'orange']

pop需要制定下标并且有返回值

fruit_list = ['apple', 'banana', 'pear', 'orange']
res = fruit_list.pop(0) # 删除apple
print(res)
print(fruit_list) # 输出
apple
['banana', 'pear', 'orange']

7、len()计算列表长度:此方式适合于列表和元祖

fruit_list = ['apple', 'banana', 'pear', 'orange']
res = len(fruit_list)
print(res) # 输出
4

8、列表多重赋值技巧:此方式适合于列表和元祖

多重赋值其实是一种快捷方法,让你在一行代码中,用列表中的值为多个变量赋值,比如:

fruit_list = ['tom', '', 'M']
name, age, sex = fruit_list
print(name, age, sex) # 输出
tom 19 M

下面我们对列表和元祖进行一个简单总结:

1、列表和元祖中的元素是有序的

2、列表种的元素是可变的,而元祖是不可变的

3、列表和元祖都是可嵌套的

Python入门2的更多相关文章

  1. python入门简介

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  2. python入门学习课程推荐

    最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...

  3. Python运算符,python入门到精通[五]

    运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...

  4. Python基本语法[二],python入门到精通[四]

    在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...

  5. Python基本语法,python入门到精通[二]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...

  6. visual studio 2015 搭建python开发环境,python入门到精通[三]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...

  7. python入门教程链接

    python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...

  8. Python学习【第二篇】Python入门

    Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...

  9. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  10. Python入门版

    一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...

随机推荐

  1. UIKit框架之UIGestureRecognizer

    ---恢复内容开始--- 1.继承链:NSObject 2.UIGestureRecognizer的子类有以下: UITapGestureRecognizer :点击 UIPinchGestureRe ...

  2. 7.2.3 使用RenderTargetBitmap类生成图片

    RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实际的编程中通常会利用RenderTargetBitmap类来对U ...

  3. 远程联机linux主机

    远程联机linux主机 推荐使用 ssh  如 ssh user@www.abc.com(ssh使用公钥+私钥非对称加密,数据传输安全,不要使用telnet) 传输文件:sftp 或者 scp 若想使 ...

  4. 将string转换成char* (转)

    原文:http://blog.sina.com.cn/s/blog_786ce14d01014lpr.html string 是c++标准库里面其中一个,封装了对字符串的操作把string转换为cha ...

  5. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  6. PHP中CURL方法curl_setopt()函数的一些参数

    bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,v ...

  7. Linux 压缩和解压缩常用命令

    主要记录tar,zip,gzip,bzip2,rar等常用命令,对.tar..gz..tar.gz..tgz..bz2..tar.bz2..zip..rar这8种压缩文件的操作. 1. tar 命令 ...

  8. C# 获取 oracle 存储过程的 返回值1

    /// <summary> /// 返回对应表的模拟自增字段值 /// </summary> /// <param name="tablename"& ...

  9. jQuery HTML

    alert("Text: " + $("#test").text());获取text alert("HTML: " + $("#t ...

  10. 返回顶部demo

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...