2.3字符串的知识

"my name is liming"
"我的名字是李明"

'i come from china'
'我来自中国' 

 双引号 单引号都可以

双引号中可以使用单引号、单引号中也可以使用双引号

但是双引号中使用双引号、单引号中使用单引号会报错

"my name is liming"
'i come from china'  没有问题

"my name is 'liming'"
'i come from "china"' 没有问题

"my name is "liming""
'i come from 'china'' 出现错误

使用方法更改字符串大小写 

python的字符串,可以使用.upper()和lower(),变成大写形式和小写形式

message = "My Name is LiMing"

print(message.upper())
print(message.lower())

输出结果如下:
MY NAME IS LIMING
my name is liming

 怎样在字符串中使用变量

name = "liming"
country = "china"
message = f"my name is {name},i come from {country}"
print(message)

输出结果如下:my name is liming, i come from china

f是format的缩写 意思是格式字符串

使用大括号的形式 可以使用外面的变量

字符串中可以使用空白符号

在字符串中如果用\t表示制表符、用\n可以表示换行符

 

print("hello \t world,\t i love python")
输出结果如下:
hello  world,   i love python

print("hello \nworld,\ni love python")
输出结果如下:
hello
world,
i love python

 

strip方法可以删除字符串前后空白

data = " python "
data = data.strip()
print(data)

输出结果如下,两边没有空格了:
python

 

 

字符串的组装和拼接

 

posted @ 2023-06-29 10:32  等风来lzd  阅读(43)  评论(0)    收藏  举报