Common variable types in python

value

a = 10
b = 123

string

In python, the strings enclosed in single quotes '' and double quotes "" are all strings, and the strings are not enclosed in quotes. Strings are the most commonly used data types and are used to represent a piece of text information.
For example:
a = '123'
b = "123"
Addition operations can be used between strings. If two strings are added, the two strings will be automatically spliced ​​into one.
The '123'+'abc'
string does not need to be added with other types.
Single quotes and double quotes cannot be used across lines
Use triple quotes to represent a long string''' """
Triple quotes can wrap lines and preserve the formatting in the string

s1 = '锄禾日当午,\
汗滴禾下土,\
谁知盘中餐,\
粒粒皆辛苦'
print(s1)
print('=====================================')
s2 = '''锄禾日当午,
汗滴禾下土,
谁知盘中餐,
粒粒皆辛苦'''
print(s2)

The output results are as follows:
insert image description here
To sum up:
use four common ways to output strings

# 创建一个变量来保存你的名字
name = '孙悟空'
# 使用四种常见方式输出,欢迎 xxx 光临
# 1.拼串
print('欢迎'+name+'光临!')
# 2.多个参数
print('欢迎',name,'光临!')  # 这个方式有点特殊拼接后会自动在 name前后加一个空格。
# 3.占位符
print('欢迎%s光临!,'%name)
# 4.格式化
print(f'欢迎{
      
      name}光临!')

Output result:
insert image description here
copy of string (multiply string and number)

a = 'abc'
a = a * 2 #解释器将字符串重复指定的次数并返回
print(a)

insert image description here

Boolean value (bool)

There are only two types of Boolean data, one True means true and the other False means false. Mainly used to make logical judgments.
a = True
b = False
Boolean values ​​are actually integers, True corresponds to 1, False corresponds to 0

None (empty value)

None is used specifically to indicate absence. Not used much
b = None
print(b)
output: None

type checking

a = 123  	#数值
b = '123'   #字符串
print(a)
print(b)

insert image description here
According to the above output results, it is impossible to determine what type a and b are.

With type checking, you can check the type of a specified value (variable). Note that variables in python have no types.
Type() is used to check the type of the value.
The function will return the result of the check as the return value, and a variable can also be used to receive the return value of the function.

a = 123
b = '123'
print(a)
print(b)
print(type(a))
print(type(b))

insert image description here
You can also pass the literal value directly to the type() function

print(type(1))
print(type(1.5))
print(type(True))
print(type('Hello'))
print(type(None))

insert image description here

Guess you like

Origin blog.csdn.net/adminstate/article/details/130784130