Python基础学习
作者:互联网
1、字符串类型的变量内容有换行的时候,可以用三个单引号或者三个双引号括起来
str=''' I am a student. Nice to meet you! '''
print(str)
str=""" I am a student. Nice to meet you! """ print(str)
2、对字符串中的字符提取时通过索引进行提取,有两种方式
- 从前往后索引,下标从0开始
- 从后往前索引,下标从-1开始
3、常用的字符串方法
1 star = " NEPTUNE " 2 #长度为9 3 print(len(star)) 4 #长度为7 5 print(len(star.strip())) 6 7 say_hi = "Hello World!" 8 #输出Hello Kitty! 9 print(say_hi.replace("World", "Kitty")) 10 11 say_hi = "Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Pluto" 12 #变成了字符串数组['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'] 13 arr=say_hi.split(",") 14 print(arr) 15 16 book_name = "Men Are from Mars, Women Are from Venus" 17 #True 18 is_exist = "Mars" in book_name 19 #False 20 is_exist = "Mars" not in book_name字符串常用方法
4、在字符串中引用变量的值
1 name = 'Earth' 2 age = 4.543E9 3 #输出结果为:My name is Earth, 4543000000.0 years old 4 print(f"My name is {name}, {age} years old")
标签:name,Python,基础,学习,字符串,hi,str,Mars,print 来源: https://www.cnblogs.com/huangyinge/p/15798607.html