Python编程 字符串组成方式

  • 作者简介:一名在校计算机学生、每天分享Python的学习经验、和学习笔记。 

  •  座右铭:低头赶路,敬事如仪

  • 个人主页:网络豆的主页​​​​​​

目录

 前言

一.字符串

1.字符串组成方式(掌握)

例子:

 方式一

方式二

方式三


 前言

本章将会讲解Python编程中字符串的组成方式,重点掌握。

一.字符串

1.字符串组成方式(掌握)

①字符串相加
②字符串格式化
 %s %d %f
 str.format()
 python3.6.4以上 引入 f''
print("1"+"2")      #得12   字符串用+做拼接 

例子:

 方式一

弊端:%类型需要考虑数据类型

name = "doudou"
age = 20
#挖坑/填坑
# %s----> str          名字
# %d----> decimal      年龄
# %f----> float        浮点数
print("%s年龄为**%d"%(name,age))

方式二

name = "doudou"
age = 20

# str.format()     S.format(*args, **kwargs) -> str 字符串的一个方法返回字符串
#{}——————> 占坑           好处1:不用考虑数据类型 好处2:可以切换位置
print("{1}年龄为{0}".format(age,name))

方式三

name = "doudou"  #python 3.6.4以上版本
age = 20
# f''
# {} --> 占坑
print(f"{name}年龄为{age}")

创作不易,求关注,点赞,收藏,谢谢~

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
Python,有多种方式可以组合字符串。一种常见的方式是使用字符串相加操作符(+)来拼接字符串。例如,可以使用以下代码将两个字符串组合在一起: ```python str1 = "Hello" str2 = "World" result = str1 + " " + str2 print(result) # 输出:Hello World ``` 另一种方式是使用字符串的`join()`方法。这个方法接受一个可迭代对象作为参数,并将其的元素用指定的字符串连接起来。例如,可以使用以下代码将一个列表字符串元素连接起来: ```python list_words = \["I", "love", "Python"\] result = " ".join(list_words) print(result) # 输出:I love Python ``` 还有一种方式是使用字符串的格式化方法,例如使用`%`操作符或`str.format()`方法。这些方法允许在字符串插入变量或表达式的值。例如: ```python name = "Alice" age = 25 result = "My name is %s and I am %d years old." % (name, age) print(result) # 输出:My name is Alice and I am 25 years old. name = "Bob" age = 30 result = "My name is {} and I am {} years old.".format(name, age) print(result) # 输出:My name is Bob and I am 30 years old. ``` 从Python 3.6.4版本开始,还引入了一种新的字符串格式化方式,使用f字符串(f-string)。它允许在字符串直接嵌入变量或表达式,并使用大括号{}包围。例如: ```python name = "Charlie" age = 35 result = f"My name is {name} and I am {age} years old." print(result) # 输出:My name is Charlie and I am 35 years old. ``` 这些是在Python组合字符串的几种常见方式。根据具体的需求和场景,可以选择适合的方式来组合字符串。 #### 引用[.reference_title] - *1* [Python编程 字符串组成方式](https://blog.csdn.net/yj11290301/article/details/127777020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [五种方式Python拼接字符串的正确方法](https://blog.csdn.net/m0_74872863/article/details/129699625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [学习python字符串组合](https://blog.csdn.net/m0_66722163/article/details/122891107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网络豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值