首页 > 编程 > Python > 正文

Python -- 1. 变量和简单数据类型

2019-11-08 01:57:28
字体:
来源:转载
供稿:网友

一:变量和简单数据类型 1. 变量: 变量名只能包含字母、数字和下划线。 变量名不能包含空格、Python关键字。 大小写区分

message = "Hello Python world!"PRint(message)

2. 字符串: 字符串中包含引号和撇号

print('I told my friend, "Python is my favorite language!"')print("The language 'Python' is named after Monty Python, not the snake.")print("One of Python's strengths is its diverse and supportive community.")

以首字母大写的方式显示每个单词

name = "ada lovelace"print(name.title())

合并(拼接)字符串

first_name = "ada"last_name = "lovelace"full_name = first_name + " " + last_nameprint(full_name)

使用制表符或换行符

print("/tPython")print("/nPython")

删除空白 删除字符串末尾空白,可使用方法rstrip() 删除字符串开头的空白,使用lstrip() 同时剔除字符串两端的空白,使用strip()

s1 = " message "print(s1)print(s1.rstrip())print(s1.lstrip())print(s1.strip())

3. 数字 整数 可对整数执行加(+ )减(- )乘(* )除(/ )乘方(**)运算

浮点数 Python将带小数点的数字都称为浮点数

整数用作字符串 调用函数str()

age = 23message = "Happy " + str(age) + "rd Birthday!"print(message)

4. 注释 注释用井号(# )标识


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表