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 = 2018print(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}')
翻译自: https://www.journaldev.com/23642/python-concatenate-string-and-int
python字符串转整数
python字符串转整数_Python连接字符串和整数相关推荐
- python中替换字符串中字符_python替换字符串中的某个字符
python_split_strip_replace使用方法 使用python时会经常要对字符串做一些处理,比如:分割字符串.去掉空格.替换字符串 中的某个字符等,下面介绍下这几个功能的使用. 一. ...
- [转载] python字符串数组字典_Python:字符串、列表、元组、字典
参考链接: Python字符串| ascii_uppercase 字符串: 所有方法都修改不了字符串的值,字符串还是原值:但可以重新赋值:使用字符串方法有返回值 循环字符串的用法: 字符串方法: Na ...
- python输出字符串拼接数字_python实现字符串和数字拼接
python实现字符串和数字拼接 如下所示: 将i前面加str(i)就可以了 补充拓展:python 连接字符串和数字 python 连接字符串和数字的问题: 首先要说的是,python是强类型的语言 ...
- python字符串怎么表示_python中字符串的几种表达方式(用什么方式表示字符串)...
原博文 2018-11-21 18:45 − 说明: 今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. pytho ...
- python怎么用split字符串全部分开_python实现字符串完美拆分split()的方法
函数:split() 例子 我们想要将以下字符串rule进行拆分.字符串表示的是一个规则,由"-"得到"-".我们需要将规则中的条件属性与取值分别提取出来,存放 ...
- python去除字符串两边空格_Python去除字符串两端空格的方法
Python去除字符串两端空格的方法 目的 获得一个首尾不含多余空格的字符串 方法 可以使用字符串的以下方法处理: string.lstrip(s[, chars]) Return a copy of ...
- string截取某个字符串之前的_python String字符串操作
今天学习python的String字符串操作,并记录学习过程欢迎大家一起交流分享. 首先新建一个python文件命名为py3_string.py,在这个文件中进行字符串操作代码编写(如下为代码,文后有 ...
- python判断回文_Python实现判断一个整数是否为回文数算法示例
本文实例讲述了Python实现判断一个整数是否为回文数算法.分享给大家供大家参考,具体如下: 第一个思路是先将整数转换为字符串,再将字符串翻转并与原字符串做比较 def isPalindrome(se ...
- python oracle数据库操作_Python连接oracle数据库的基本操作
1 #导入cx_Oracle模块 2 importcx_Oracle as cx3 4 #连接数据库 5 db = cx.connect('username/password@host/orcl') ...
最新文章
- strtok和strtok_r
- DARPA 2020财年研发预算 人工智能应用研究投资急剧增长
- 你真的了解lambda吗?一文让你明白lambda用法与源码分析
- python科学计数法转换_柳小白Python学习笔记35 Excel之科学计数法类型转换及数据选取1...
- [TJOI2012] 旅游(树的直径)
- datagrid显示mysql_WPF DataGrid显示MySQL查询信息,且可删除、修改、插入 (原发布 csdn 2018-10-13 20:07:28)...
- VC连接access
- JavaScript基础之Array对象和Boolean对象
- mac的终端通过ssh远程连接Linux服务器
- html中抓取span中的值,JavaScript如何获取span元素的文本?
- 电脑练习打字软件_极速字根练习软件电脑版下载-极速字根练习软件免费版v0.1 最新版...
- DNE-1 测试方法
- oracle技术之一次RMAN备份报错的诊断过程(五)
- Hibernate validator 官网前言中文版
- STM32 内部Flash读写 程序源码 [已验证]
- ipad协议835最新版
- html chm 64,Win7 64位下的CHM
- debounce、throttle、useDebounce、useThrottle
- 【山外笔记-工具框架】SVN版本控制系统
- 天润IVR语音自助系统破解电商验证难题