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