python字符串转整数_Python连接字符串和整数

python字符串转整数

Python supports string concatenation using + operator. In most of the programming languages, if we concatenate a string with an integer or any other primitive data types, the language takes care of converting them to string and then concatenate it. However, in Python, if you try to concatenate string and int using + operator, you will get a runtime error.

Python支持使用+运算符进行字符串连接。 在大多数编程语言中,如果我们将字符串与整数或任何其他原始数据类型连接在一起,则该语言会负责将它们转换为字符串,然后对其进行连接。 但是,在Python中,如果尝试使用+运算符将字符串和int连接起来,则会出现运行时错误。

Python连接字符串和整数 (Python Concatenate String and int)

Let’s look at a simple example to concatenate string and int using + operator.

让我们看一个使用+运算符将字符串和int连接起来的简单示例。

s = 'Year is '

y = 2018

print(s + y)

Output:

输出:

Traceback (most recent call last):
  File "/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in <module>
    print(s + y)
TypeError: can only concatenate str (not "int") to str

So how to concatenate string and int in Python? There are various other ways to perform this operation.

那么如何在Python中连接字符串和整数呢? 还有多种其他方式可以执行此操作。

使用str()函数 (Using str() function)

The easiest way is to convert int to a string using str() function.

最简单的方法是使用str()函数将int转换为字符串。

print(s + str(y))

Output: Year is 2018

产出: Year is 2018

使用%运算符 (Using % Operator)

print("%s%s" % (s, y))

使用format()函数 (Using format() function)

We can use string format() function too for concatenation of string and int.

我们也可以使用字符串format()函数来连接字符串和整数。

print("{}{}".format(s, y))

使用f弦 (Using f-strings)

If you are using Python 3.6 or higher versions, you can use f-strings too.

如果您使用的是Python 3.6或更高版本,则也可以使用f字符串

print(f'{s}{y}')
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23642/python-concatenate-string-and-int

python字符串转整数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用int()方法将字符串换为整数类型。这个方法接受一个字符串对象作为参数,并返回一个整数对象。使用示例如下: ```python # 字符串对象 age = "18" print(age) # 将字符串换为整数 int_age = int(age) print(int_age) ``` 输出结果为: ``` 18 18 ``` 请注意,第一行输出的是一个字符串对象,而第二行输出的是一个整数对象。这是因为通过int()方法将字符串换为整数。 如果你想在换后的整数上执行数值操作,例如加法、减法等,你需要确保将所有的操作数都换为整数。否则,你可能会遇到错误。例如,下面的代码会报错: ```python age = "18" print(age + 2) ``` 这会产生以下报错信息: ``` TypeError: can only concatenate str (not "int") to str ``` 为了避免这个错误,你需要先将age对象换为整数,然后再执行任何数值操作: ```python age = "18" age_int = int(age) print(age_int + 2) ``` 输出结果为: ``` 20 ``` 需要注意的是,当将浮点数(带有小数部分的数)作为参数传递给int()方法时,它会将浮点数四舍五入为最接近的整数。例如,int(7.9)的输出结果是7。 然而,如果你尝试将作为字符串对象的浮点数换为整数,会遇到错误。例如,int("7.9")会报错,因为无法将字符串对象中的浮点数换为整数。 类似地,当将单词作为参数传递给int()方法时,也会遇到相同的错误。例如,int("one")会报错。 总结来说,如果你想将一个字符串换为整数类型,可以使用Pythonint()方法。它会将合法的整数字符串换为整数对象,同时可以处理浮点数和其他不合法的字符串参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值