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第二天的学习内容,希望能对你有所帮助,祝你学有所成!!!

最新文章

  1. 同个项目写webservice引用EF出现的问题
  2. Webpack 中文指南
  3. [05]APUE:标准 I/O 库
  4. 【poj3764】 The xor-longest Path
  5. android service 学习
  6. [MySQL优化案例]系列 — slave延迟很大优化方法
  7. [原创]ie6,7中td和img之间有间隙
  8. Android学习总结——TextView跑马灯效果
  9. 深入GDI图像显示
  10. 使用CASE表达式替代SQL Server中的动态SQL
  11. 一个关于Linux Bridge配置的吐嘈
  12. 关于scanf的返回值
  13. kali自动化清理缓存和日志
  14. 2016大连网络赛 Weak Pair
  15. SQL Server事务、隔离级别详解(二十九)
  16. 关于换了手机后,导致原来连的fiddler抓不到新手机上的包的解决方法
  17. Zookeeper的安装部署
  18. BFC特性下多栏自适应布局
  19. DWR第三篇之逆向Ajax升级
  20. [转]你可能不知道的五个强大HTML5 API

热门文章

  1. python信息收集(二)
  2. c++使用cin、cout与c中使用scanf、printf进行输入输出的效率问题
  3. 高级数据结构---赫(哈)夫曼树及java代码实现
  4. Android-网页解析-gson的使用
  5. docker中安装宝塔面板教程
  6. python连接mysql数据表查询表获取数据导入到txt中
  7. 本周ASP.NET英文技术文章推荐[02/03 - 02/16]:MVC、Visual Studio 2008、安全性、性能、LINQ to JavaScript、jQuery...
  8. Nginx入门及如何反向代理解决生产环境跨域问题
  9. JS省城级联
  10. 图论--最短路--SPFA