day_02

  • 使用方法修改字符串的大小写

将字符串首字母变成大写

>>> name = "ada lovelace"
>>> print(name.title())
Ada Lovelace

将字符串全部变成大写

>>> print(name.upper())
ADA LOVELACE

将字符串全部变成小写

>>> print(name.lower())
ada lovelace
  • 合并字符串

使用 + 来合并 first_name,last_name,空格

>>> first_name = "ada"
>>> last_name = "lovelace"
>>> full_name = first_name + " " +last_name
>>> print(full_name)
ada lovelace

乘热打铁,使用title()来组装一个字符串

>>> print("Hello,"+ " " + full_name.title() +"!")
Hello, Ada Lovelace!

也可以将整条信息储存在一个变量中

>>> message = "Hello,"+ " " + full_name.title() +"!"
>>> print(message)
Hello, Ada Lovelace!
  • 使用制表符或换行符来添加空白

在字符中添加制表符,可以使用字符组合\t

>>> print("Python")
Python
>>> print("\tPython")
Python

在字符中添加换行符,可以使用字符组合\n

>>> print("I\nlove\nPython")
I
love
Python

使用换行符加上制表符

>>> print("I\n\tlove\n\tPython")
I
love
Python
  • 删除空白

使用方法rstrip(),这种方法只是暂时的

>>> a_word = 'python '
>>> a_word
'python ' #输入的时候有一个空格符
>>> a_word.rstrip()
'python' #消除了空白
>>> a_word
'python '

要想永久删除空格,还需将删除的操作重新赋值到原来的变量里面

>>> a_word = a_word.rstrip()
>>> a_word
'python'

rstrip()剔除右边的空白

lstrip()剔除左边的空白

strip()剔除全部空白

>>> a_word = ' python '  #左边和右边都有空白
>>> a_word.rstrip()
' python'
>>> a_word.lstrip()
'python '
>>> a_word.strip()
'python'
  • 使用字符串是避免语法错误

撇号位于两个双引号之间,故能识别出整个字符串

>>> message = "I'm a student."
>>> print(message)
I'm a student.

但是使用单引号时

>>> message = 'I'm a student.'
SyntaxError: invalid syntax

Python无法正确地确定字符串结束的位置

  • 数字

整数

在Python中可以对整数进行+ - * / 运算

>>> 1+1
2
>>> 3-5
-2
>>> 3-2
1
>>> 3/2
1.5
>>> 8*9
72

Python使用两个乘号(**)进行乘方运算

>>> 8*9
72
>>> 3**3
27
>>> 2**3
8
>>> 5**5
3125

浮点数

使用浮点数是无需考虑其他行为。只需输入要使用的数字

>>> 1.2+5
6.2
>>> 1.2+36.3
37.5
>>> 1.7+9.6
11.299999999999999
>>> 1.6+1.6
3.2
>>> 0.2+0.1
0.30000000000000004
>>> 2*0.2
0.4
>>> 1*0.3
0.3

有时结果包含的小数点是不确定的

但是无需担心,接下来的学习中会解决此类问题

  • 使用函数str()避免类型错误

有时候你希望

>>> age = 22
>>> message = "Happy" + " " + age +"rd Birthday!!"

你会希望输出"Happy 22rd Byrthday!!"

但是,很遗憾上述代码会引发错误

>>> age = 22
>>> message = "Happy" + " " + age +"rd Birthday!!"
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
message = "Happy" + " " + age +"rd Birthday!!"
TypeError: can only concatenate str (not "int") to str

这是一个类型错误,Python发现你使用了一个值为整数(int)的值,它不知道如何解读这个值,它认为这个变量表达的可能是22,也可能是字符2和字符2,为此,在调用变量是。你需要调用str()函数来将这个变量变成字符串。

>>>age = 22
>>> message = "Happy" + " " + str(age) + "rd Birthday!!"
>>> print(message)
Happy 22rd Birthday!!
  • python之禅

在IDLE中运行import this就可以得到Python之禅

>>> import this
The Zen of Python, by Tim Peters Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

以上就是Python第二天的学习内容,希望能对你有所帮助,祝你学有所成!!!

Python基础语法day_02——字符串规则的更多相关文章

  1. 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)

    """ 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...

  2. python基础语法_字符串编码

    Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html   Python常见字符编码间的转换   在字符串写入文件时,有时会因编码问题导致无法 ...

  3. python基础语法之字符串

    1 字符串中*的使用 *可以使字符串重复n次 print('hello world ' * 2) # hello world hello world 2 索引获取字符串的字符元素 print('hel ...

  4. python基础语法及知识点总结

    本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...

  5. Python基础语法题库

    引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...

  6. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  7. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  8. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

  9. Python 基础语法

    Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...

随机推荐

  1. SpringCloud(二)笔记之Eureka

    Eureka包含两个组件:Eureka Server和Eureka Client Eureka Server:提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册 Eureka ...

  2. 用SQL查询分析实现类似金蝶K3的收发存明细表

    使用SQL查询分析实现类收发存的报表,原始需求在 另外一篇文章 的第四部分.下图是实现需求. 一.准备 删除临时表 [buy]判断是否存在临时表,存在则删除[/buy] if OBJECT_ID('t ...

  3. linux上Docker安装gogs私服亲测(详解)

    一.前言 有网友问我为什么要使用私服,可能大部分人都不是太懂,网上那么多存储仓库而且好用方便,但是你想过没有如果企业中的项目,放在人家的仓库上这个安全性不是太好,所以说一般企业都会有自己的私服.本章教 ...

  4. 大数据并行计算框架Spark

    Spark2.1. http://dblab.xmu.edu.cn/blog/1689-2/ 0+入门:Spark的安装和使用(Python版) Spark2.1.0+入门:第一个Spark应用程序: ...

  5. 使用cat命令清空文件

    比如要清空 /www/aaa.txt cat /dev/null > /www/aaa.txt; 即可.

  6. Dockerfle创建镜像

    简介 Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行. 一般的,Dockerfile 分为四部分:基础镜像信息.维护者信息.镜像操作指令和容器启动时执行指令. # This ...

  7. CG-CTF(4)

    CG-CTF https://cgctf.nuptsast.com/challenges#Web 续上~ 第十六题:bypass again 代码分析: 当a不等于b,且a和b的md5值相同时,才会返 ...

  8. 点击 QTableView,触发事件

    Here is an example of how you can get a table cell's text when clicking on it. Suppose a QTableView ...

  9. jQuery动态时钟

    效果图: 引用的jQuery.js自己百度 代码: <!DOCTYPE html> <html> <head> <meta charset="utf ...

  10. Tomcat7 启动慢的问题解决

    [问题] 由于上面标记部分,导致启动耗时将近160s,不能忍! [原因] 此于jvm环境配置有关,请打开jvm安装目录中jre/lib/security/java.security文件,找到secur ...