Home Backend Development Python Tutorial python convert string using str & repr

python convert string using str & repr

Dec 05, 2016 pm 01:27 PM
python repr str the difference

可能比较 low 还是记录一下:

str 和 repr的使用过程

  1. str 是一个类型 (int, long 类似), 同样她也可以作为一个工厂方法 实例一个 string
  2. repr 是python 内置的函数, 用于保留一个 打印值在python 代码片段里的真实状态

好,以上全是废话

>>> a = 1
>>> a + ""
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
<ipython-input-5-ebf3ab7f3a34> in <module>()
----> 1 a + ""

TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> a = 1
>>> repr(a) + ""

Copy after login

可以看到, 我们可以通过使用 str 和 repr 来转换字符串

但是,str 只能提供一个 元string来做转换, 不可以是一个变量(她不具备执行变量的能力)

repr 是一个函数, 所以实际上是传参, 可以是变量和string

好多人都知道str()能把123数字转成字符串,python里的str()甚至还能把列表、字典等对象转成字符串。这都好理解,可是一旦把str()和repr()放在一起,大家就全都不淡定了-_-!

来看一段代码,仍是在IDLE里交互:

  >>> str('hello')
  'hello'
  >>> repr('hello')
  "'hello'"

  >>> str('你好')
  '\xc4\xe3\xba\xc3'
  >>> repr('你好')
  "'\\xc4\\xe3\\xba\\xc3'"

Copy after login

先看前两句:英文的'hello'在str()后仍是'hello',可是在repr()后就变成了"'hello'"。这就说明,str()返回的就是字符串本身,而repr()虽然返回的也是字符串,但它是一个标准字符串,官方解释比较绕,我来解释下吧。repr是representation及描述的意思,不是对人的描述,而是对python机器的描述,也就是它会将某物返回一个它在python中的描述。说人话:repr(obj)告诉我们obj这个变量在背地里是什么样子,在背地里是怎么被python处理被python"玩弄"的。

在python里,我们总会被眼睛欺骗。编辑器里显示的东西,并不总是它原本的面貌。python为了方便,总是表面上一套,背地里又一套。

再来理解后两句:中文的'你好'在str()后变成了编码'\xc4\xe3\xba\xc3',在repr()后变成了"'\xc4\xe3\xba\xc3'"。`都加上了转移符变成\,相当于把字符串中的内容都“标准化”了。至于'变成"`只是为了说明repr()返回的是一个经过处理的新字符串。

print后的str()和repr()

来看代码:

  >>> print str('你好')
  你好
  >>> print repr('你好')
  '\xc4\xe3\xba\xc3'
Copy after login

之前str('你好')显示的是'\xc4\xe3\xba\xc3',而一经过print,就变成了正确的'你好'。上面说过了,命令行里直接输入一个变量,显示的是它在python后台存储的数据;而用print出来的东西,会显出出尽量友好、让人能看懂的东西。
理解了这个,对print这两个结果的不同,也就全然理解了。然后也就放弃print作为考据的心了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

See all articles