python3.7入门系列三 变量、字符串和数字

一、变量

定义一个变量message,值为hello python,数据类型为字符串,执行后输出 hello python

>>> message = "hello python"
>>> print(message)
hello python

变量命名:

  1. 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为message_1,但不能将其命名为1_message。
  2. 变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。
  3. 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print
  4. 变量名应既简短又具有描述性。例如,name比n好,student_name比s_n好,name_length比length_of_persons_name好。
  5. 慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。

二、字符串

用一对引号括起来的内容(可以是一对单引号和一对双引号)

一对单引号字符串内可以保护双引号,一对双引号字符串内可以保护单引号。但是单引号字符串内不能再有单引号,双引号字符串内不能有双引号,否则会引起语法错误。

字符串拼接用 + ,如

>>> first_name = "bill"

>>> last_name = "gates"

>>> name = first_name + ", " + last_name

>>> print(name)

bill, gates

>>> print(name.title())

Bill, Gates

 

字符串不能和数字相加(也不能与其他类型的变量或值相加),需要先将数字变为字符串,如

>>> message = 'age is ' + 18

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: can only concatenate str (not "int") to str

>>> message = 'age is ' + str(18)

>>> print(message)

age is 18

这里的str()函数可以将数字转变为字符串,避免类型错误

 

转义符\: \t 制表符  \n换行符

>>> msg = 'it\'s a dog'

>>> print(msg)

it's a dog

python三引号

三引号允许一个字符串跨多行
message = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
"""
print (message)

字符串截取、in/not in

msg = "hellopython"
msg[2:5]    # 取从第三个开始到第五个的字符,不包含第五个字符
msg[0:-1]   # 取第一个到倒数第二个的所有字符
msg[2:]     # 取从第三个开始的后的所有字符
>>> msg = "hellopython"
>>> print(msg[2:5])
llo
>>> print(msg[0:-1])
hellopytho
>>> print(msg[2:])
llopython

>>> msg = "hello python"
>>> print('py' in msg)
True
>>> print('hi' in msg)
False
>>> print('hi' not in msg)
True

python内建的对字符串的函数

isalpha()      # 如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False
isdigit()      # 如果字符串至少有一个字符且只包含数字则返回 True 否则返回 False
isnumeric()    # 如果字符串至少有一个字符且只包含数字字符,则返回 True,否则返回 False
islower()      # 如果字符串至少有一个字符且所有的字符都是小写,则返回 True,否则返回 False
isupper()      # 如果字符串至少有一个字符且所有的字符都是大写,则返回 True,否则返回 False
istitle()      # 如果字符串是标题化的(见 title())则返回 True,否则返回 False
lstrip()       # 去掉左右两边的空格
rstrip()       # 去掉右边的空格
strip()        # 去掉左右两边的空格
zfill(width)   # 返回长度为 width 的字符串,原字符串右对齐,不足width长度的前面填充0
upper()        # 转换为大写
lower()        # 转换为小写
title()        # 首字母大写
len(str)       # 获取字符串长度
max(str)       # 返回字符串 str 中最大的字母
min(str)       # 返回字符串 str 中最小的字母

>>> msg = "hellopython"
>>> msg.isalpha()
True
>>> msg.isdigit()
False
>>> msg.isnumeric()
False
>>> msg.islower()
True
>>> msg.isupper()
False
>>> msg.istitle()
False
>>> msg.lstrip()
'hellopython'
>>> msg.rstrip()
'hellopython'
>>> msg.strip()
'hellopython'
>>> msg.zfill(15)
'0000hellopython'
>>> msg.upper()
'HELLOPYTHON'
>>> msg.lower()
'hellopython'
>>> msg.title()
'Hellopython'
>>> len(msg)
11
>>> max(msg)
'y'
>>> min(msg)
'e'

三、数字

加+、减-、乘*、除/、整除//、幂**

>>> 3 + 2
5
>>> 3 - 2
1
>>> 3 * 2
6
>>> 3 ** 2
9
>>> 3 / 2
1.5
>>> 3 // 2
1

浮点数,并不确定

>>> 0.1 + 0.2

0.30000000000000004

四、注释

# 单行注释,井号后面的内容都是注释的内容,执行时会被python忽略

一对三个单引号或一对三个双引号包起来一段内容,可以对大段的内容做注释

如,

# 这是单行注释

"""
大段注释
大段注释
大段注释
"""

# 或

'''
大段注释
大段注释
大段注释
'''

本文内容到此结束,更多内容可关注公众号和个人微信号:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值