Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with string in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Python how to carry out string processing, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

Question: How does the string not escape?

The first one: \\Escape the escape symbol\Escape it with\\

Method 2: add lowercase r before the string, note that it cannot be used with variable strings

print(r"Liu Jinyu\\nProgramming")

Method 3: repr(string) function

>>> x="Liu Jinyu\nProgramming"

>>> print(repr(x))

'Liu Jinyu\nProgramming'

The string after the repr function output, enclosed in quotes.

2) How do you see the length of a string?

Here's how to use len (string) function

Note:

1. The length of the output\n is regarded as a character.

Single quotes contained in the string result output by the repr(string) function will be counted within the length of the string.

How do I get the length of a number?

str(number): converts number type to string type

>>> a=2341

>>> print(len(a))

Traceback (most recent call last):

File "", line 1, in

TypeError: object of type 'int' has no len()

>>> print(len(str(a)))

4

IV. How to splice strings?

Method 1: Use the parameter output function of print function

>>> x="Liu Jinyu"

>>> y="Programming"

>>> print(x,y)

Liu Jinyu Programming

Method 2: Use + to concatenate strings

>>> print(x+y)

Liu Jinyu Programming

>>> z=x+y

>>> print(z)

5. How to write a string in multiple lines?

Use "" or """

x="""Liu Jinyu

programming

Programming to create cities """

Note here is the way three quotes, pay attention to the middle of the output line, will also be in the result of the output line, the output format unchanged. If escape characters are encountered, they will also be escaped.

Write with symbol continuation:

\function is to continue the line writing, here notice, the next line of spaces will be actually output

VI. Summary and emphasis

1, master the length of the string to obtain.

2. Master the conversion of numbers to strings.

Understand how strings are not escaped. Use escape characters.

4. Master the method of writing multi-line strings

Source code for this section:

x="""Liu Jinyu\nProgramming to create cities"""

print(x)

input()

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report