# Python specifying variable types# int() 从整数文字、浮点文字(Delete decimals)或字符串文字(提供字符串表示整数)构造整数# float() 从整数文字、浮点文字或字符串文字构造浮点数(提供字符串表示浮点数或整数)# str() 从多种数据类型构造一个字符串,包括字符串、整理文字和浮点文字# int
x =int(1)# x will be 1
y =int(2.8)# y will be 2
z =int("3")# z will be 3# float
x2 =float(1)# x2 will be 1.0
y2 =float(2.8)# y2 will be 2.8
z2 =float("3")# z2 will be 3.0
w2 =float("4.2")# w2 will be 4.2# str
x1 =str("s1")# x1 will be 's1'
y1 =str(2)# y1 will be '2'
z1 =str(3.0)# z1 will be '3.0'# Python character string# The str comprehend# "hello" == 'hello'print("hello")print('hello')# Distribution of variables
a ="小脑斧"print(a)# Multiline string
a ="""奇变偶不变
符号看象限
"""print(a)
a ='''奇变偶不变
符号看象限
'''print(a)"""
奇变偶不变
符号看象限
"""# string array
a ="hello world!"print(a[0])""" h """# Traversal stringfor x in"饿了想吃饭":print(x)"""
饿
了
想
吃
饭
"""# string length
a ="hello python!"print(len(a))""" 13 """# Checking string
txt ="this is one words!"print("one"in txt)# 返回布尔类型# 也可以通过if判断
txt ="this is one words!"if"one"in txt:print("是的,one在txt内")""" 是的,one在txt内 """# 检查如果不是
txt ="好饿呀,想吃饭了"print("好饿"notin txt)""" False """# Sliced string# Gets an array of the specified range
b ="hello world"print(b[2:5])""" llo """# Gets the character from the start
b ="hello world"print(b[:5])""" hello """# Gets the character from the endprint(b[2:])""" llo world """# The negative indexprint(b[-5:-2])""" wor """# Modify string# Lowercase to uppercase
a =" Hello,World! "print(a.upper())""" HELLO,WORLD! """# Uppercase to lowercaseprint(a.lower())""" hello,world! """# Delete the blank spaceprint(a.strip())""" Hello,World! """# Substitution string
a ="Hello,World!"print(a.replace("H","J"))""" Jello,World! """# Split stringprint(a.split(","))""" ['Hello', 'World!'] """# concatenation of string
a ="hello"
b ="World!"
c = a + b
print(c)""" helloWorld! """
c = a +" "+ b
print(c)""" hello World! """# format string
age =20
txt ="猪猪侠今年{}岁"print(txt.format(age))""" 猪猪侠今年20岁 """# format multiple mass participation
name ="喜羊羊"
age =15
home ="羊村"
txt ="我叫{},今年{}岁了,住在{}。"print(txt.format(name,age,home))""" 我叫喜羊羊,今年15岁了,住在羊村。 """
txt ="我叫{2},今年{0}岁了,住在{1}。"print(txt.format(age,home,name))""" 我叫喜羊羊,今年15岁了,住在羊村。 """# other functions"""
1. capitalize() 将第一个字符转为大写
2. casefold() 将字符串转换为小写
3. center() 返回一个居中的字符串
4. count() 返回指定值在字符串出现的次数
5. encode() 返回字符串的编码版本
6. endswith() 如果字符串以指定的值结尾,则返回True
7. join() 将可迭代的元素连接的字符串的末尾
8. find() 在字符串中搜索指定值并返回找到它的位置
9. format() 初始化字符串中的指定值
10. index() 在字符串中搜索指定值并返回找到它的位置
"""# Test#1->使用len方法打印字符串的长度 ->11
x ="hello world"print(len(x))#2->获取字符串txt的第一个字符串 ->H
txt ="Hello world"
x = txt[0]print(x)#3->获取索引2到索引4的字符 ->llo
x = txt[2:5]print(x)#4->返回开头或结尾没有任何空格的字符串 ->helloWorld!
x =" helloWorld! "print(x.strip())#5->将txt值转换为大写 ->HELLO WORLD
txt ="Hello world"print(txt.upper())#6->将txt值转换为小写 ->hello worldprint(txt.lower())#7->用J替换字符H ->Jello worldprint(txt.replace("H","J"))#8->插入正确语法来添加年龄参数的占位符 ->txt = "My name is John, and i am {}"
age =36
txt ="My name is John, and i am {}"print(txt.format(age))
Python基础学习-04# Python specifying variable types# int() 从整数文字、浮点文字(Delete decimals)或字符串文字(提供字符串表示整数)构造整数# float() 从整数文字、浮点文字或字符串文字构造浮点数(提供字符串表示浮点数或整数)# str() 从多种数据类型构造一个字符串,包括字符串、整理文字和浮点文字# intx = int(1) # x will be 1y = int(2.8) # y will be 2z