标签:使用字符串 一个人 error: img pychar nal oba 下划线 das
1.
print("Hello Python world!")
Hello Python world!
2.
message = "Hello Python world! "
print(message)
message = "Hello Python Crash Course World!"
print(message)
Hello Python world!
Hello Python Crash Course World!
新输入的message并没有取代原来的
3.
变量的命名和使用
只包含字母,数字和下划线,但不能以数字打头
不能包含空格,但可以使用下划线来分隔其中的单词
不能以python关键词和函数名来命名 关键词(False,class,finally,is,return,
None,continue,for,lambda,try,
True,def,from,nonlocal,while,
and,del,global,not,with,
as,elif,if,or,yield,
assert,else,import,pass,
break,except,in,raise)
变量名应既简短又具有描述性
慎用小写字母l和大写字母O,因为他们可能被人错看成数字1和0
4.
字符串:可以单引号,双引号(在包含引号和撇号的句子中灵活使用)
title() 将每个单词的首字母都改为大写(why?因为在文章的title中每个单词的首字母都要大写)
name = "ada lovelace"
print(name.title())
Ada Lovelace
upper()全部大写(upper是比之前更大,so是全部大写)
lower()全部小写(lower是比之前更小,so是全部小写)
print(name.upper())
ADA LOVELACE
print(name.lower())
ada lovelace
合并(拼接)字符串
合并 A+‘ ‘+B
first_name = ‘ada‘
last_name = ‘lovelace‘
full_name = first_name + ‘ ‘+ last_name
print(full_name)
ada lovelace
拼接 "A"+B+"C"
first_name = ‘ada‘
last_name = ‘lovelace‘
full_name = first_name + ‘ ‘+ last_name
print("Hello," + full_name.title() + "!")
Hello,Ada Lovelace!
简化拼接
first_name = ‘ada‘
last_name = ‘lovelace‘
full_name = first_name + ‘ ‘+ last_name
message = "Hello," + full_name.title() + "!"
print(message)
Hello,Ada Lovelace!
使用制表符或换行符来添加空白
制表符
print("python")
print("\tpython")
python
python
注意:\t 反斜杠
print("/tpython")
/tpython
换行符
print("python")
print("\npython")
同时使用
print("python")
print("\n\tpython")
注意 print("\t\npython") 【 print("\n\tpython")】\t\n顺序不同
python
python
删除空白
确保字符串末尾没有空白,可使用rstrip(),but它是创建副本后删除副本的空白,不改变母本的空白
message = "python "
print(message)
print(message.__sizeof__())
print(message.rstrip())
print(message.rstrip().__sizeof__())
print(message)
print(message.__sizeof__())
python
58
python
55
python
58
要删除母本空白,需将结果返回到母本中
message = "python "
print(message)
print(message.__sizeof__())
message = message.rstrip()
print(message)
print(message.rstrip().__sizeof__())
print(message)
print(message.__sizeof__())
python
58
python
55
python
55
确保字符串开头没有空白,可使用lstrip(),but它是创建副本后删除副本的空白,不改变母本的空白,要删除母本空白,需将结果返回到母本中
name = ‘ Albert Einstein ‘
print(name)
print(name.__sizeof__())
print(name.lstrip())
print(name.lstrip().__sizeof__())
print(name)
print(name.__sizeof__())
Albert Einstein
68
Albert Einstein
66
Albert Einstein
68
确保字符串两端没有空白,可使用strip(),but它是创建副本后删除副本的空白,不改变母本的空白,要删除母本空白,需将结果返回到母本中
name = ‘ Albert Einstein ‘
print(name)
print(name.__sizeof__())
print(name.strip())
print(name.strip().__sizeof__())
print(name)
print(name.__sizeof__())
Albert Einstein
68
Albert Einstein
64
Albert Einstein
68
使用字符串避免错误
正确的
message = "I‘m a beginner in Python"
print(message)
I‘m a beginner in Python
错误的 (计算机不是人,它无法正确定位字符串的结束位置,这时需要我们多加注意)
message = ‘I‘m a beginner in Python‘
print(message)
File "C:/Users/25337/PycharmProjects/untitled1/app.py", line 1
message = ‘I‘m a beginner in Python‘
^
SyntaxError: invalid syntax
练习题
name = ‘Eric‘
message = "Hello " + name.title()+ ", would you like to learn some Python today?"
print(message)
Hello Eric, would you like to learn some Python today?
name = ‘jiaxin Lee‘2-5 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出来。输出应类似于下面这样(包括引号):
print(name.lower())
print(name.upper())
print(name.title())
jiaxin lee
JIAXIN LEE
Jiaxin Lee
message = ‘Albert Einstein once said, “A person who never made a mistake never tried anything new.”‘2-6 名言 2: 重复练习2-5,但将名人的姓名存储在变量famous_person 中,再创建要显示的消息,并将其存储在变量message 中,然后打印这条消息。
print(message)
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
name = ‘Albert Einstein‘Albert Einstein once said, “A person who never made a mistake never tried anything new.”
message = name+ ‘ once said, “A person who never made a mistake never tried anything new.”‘ 注意要在once前面空一格或在人名后空一格
print(message)
name = ‘ \tAlbert Einstein ‘
print(name.__sizeof__())
print(name.lstrip())
print(name.lstrip().__sizeof__())
print(name.rstrip())
print(name.rstrip().__sizeof__())
print(name.strip())
print(name.strip().__sizeof__())
name = ‘ \nAlbert Einstein ‘
print(name.__sizeof__())
print(name.lstrip())
print(name.lstrip().__sizeof__())
print(name.rstrip())
print(name.rstrip().__sizeof__())
print(name.strip())
print(name.strip().__sizeof__())
69
Albert Einstein
66
Albert Einstein
67
Albert Einstein
64
69
Albert Einstein
66
Albert Einstein
67
Albert Einstein
64
5.
数字
整数(+—*/)
print(2+3)
print(2-3)
print(2*3)
print(2/3)
5
-1
6
0.6666666666666666
乘方运算(**)
print(2**3)
6
还支持运算次序
print(2+3*4)
print((2+3)*4)
14
20
浮点数(运算后包含的小数位数可能是不确定的)
print(0.1+0.2)
0.30000000000000004
使用str()函数避免类型错误
例子如下
age = 23
message = "Happy"+ age + "rd Birthday"
print(message)
Traceback (most recent call last):
File "C:/Users/25337/PycharmProjects/untitled1/app.py", line 2, in <module>
message = "Happy"+ age + "rd Birthday"
TypeError: can only concatenate str (not "int") to str
23既可能表示数值23,有可能是数字2和3,so 我们要明确指出23的类型
age = 23
message = "Happy "+ str(age) + "rd Birthday"
print(message)
Happy 23rd Birthday
7.
注释
#向大家问好
print(‘Hello Everbody!‘)
Hello Everbody!
python学习——python编程:从入门到实践(1,2章)
标签:使用字符串 一个人 error: img pychar nal oba 下划线 das
原文地址:https://www.cnblogs.com/ljx12579/p/12532561.html