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

Strings in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Python string

Strings are the most commonly used data type in Python. We can use quotation marks ('or ") to create a string.

Creating a string is as simple as assigning a value to a variable. For example:

Var1 = 'Hello Worldwide'

Var2 = "Python Runoob"

Python access the value in the string

Python does not support single character types, and single characters are also used as a string in Python.

Python accesses substrings. You can use square brackets to intercept strings, as shown in the following example:

Instance (Python 2.0 +)

#! / usr/bin/python var1 = 'Hello Worldwide' Var2 = "Python Runoob" print "var1 [0]:", var1 [0] print "var2 [1:5]:", var2 [1:5]

The execution result of the above example:

Var1 [0]: H

Var2 [1:5]: ytho

Python string update

You can modify an existing string and assign it to another variable, as shown in the following example:

Instance (Python 2.0 +)

#! / usr/bin/python #-- coding: UTF-8-- var1 = 'Hello Worldwide' Print "update string: -", var1 [: 6] + 'runooblast'

The execution result of the above example

Update string:-Hello Runoob!

Python escape character

When you need to use special characters in characters, python escapes characters with a backslash (). The table is as follows:

Escape character description

Line continuation (at the end of a line)

\ backslash symbol

\ 'single quotation marks

\ "double quotation marks

\ a ring the bell

\ b backspace (Backspace)

\ e escape

\ 000 empty

\ nWrap

\ v Vertical tabs

\ t horizontal tab character

\ r enter

\ f Page change

\ oyy octal number, the character represented by yy, for example:\ o12 represents a line break

\ xyy hexadecimal number, the character represented by yy, for example:\ x0a represents a line break

\ other other characters are output in normal format

Python string operator

The value of the instance variable an in the following table is the string "Hello" and the value of variable b is "Python":

Operator description instance

String concatenation > > a + b 'HelloPython' repeat output string > a 2' HelloHello'

[] get the characters in the string by index > a [1]'e'

[:] intercept part of a string > a [1:4] 'ell'

In member operator-returns True > "H" in a True if the string contains a given character

Not in member operator-returns True > "M" not in a True if the string does not contain the given character

Razar R original string-original string: all strings are used directly literally, without escaping special or unprintable characters. The original string has almost exactly the same syntax as a normal string, except for the letter "r" (which can be uppercase and lowercase) before the first quotation mark of the string. > print r'\ n'\ n > print R'\ n'\ n

% format string, please see the next section

Instance (Python 2.0 +)

#! / usr/bin/python #-coding: UTF-8-a = "Hello" b = "Python" print "a + b output result:", a + b print "a 2 output result:", a 2 print "a [1] output result:", a [1] print "a [1:4] output result:" A [1:4] if ("H" in a): print "H in variable a" else: print "H is not in variable a" if ("M" not in a): print "M is not in variable a" else: print "M is in variable a" print r'\ n 'print R'\ n'

The execution results of the above procedures are as follows:

A + b output result: HelloPython

A 2 output result: HelloHello

A [1] output result: e

A [1:4] output result: ell

H is in variable a

M is not in the variable a

\ n

\ n

Python string formatting

Python supports the output of formatted strings. Although this may involve very complex expressions, the most basic use is to insert a value into a string with the string formatter% s.

In Python, string formatting uses the same syntax as the sprintf function in C.

The following is an example:

#! / usr/bin/python

Print "My name is s and weight is d kg!" ('Zara', 21)

The output result of the above example:

My name is Zara and weight is 21 kg!

Python string formatting symbols:

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

Servers

Wechat

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

12
Report