String related operations related method

String basic characteristics of

the nature of the string is: a sequence of characters. Python strings are immutable, we can not make any changes to the original string. However, you can copy the string part of the string to the newly created, to "modify the look" effect.
Python does not support single character type, but also as a single character string used.

 

Python3 direct support Unicode, a character can represent any written language in the world. Python3 default is 16 characters in Unicode encoding, ASCII code is a subset of Unicode encoding. Built-in function using the ord () can be converted to Unicode character code corresponding to; function using the built-CHR () can be converted into a decimal number corresponding to the character.

 

 

>>> ord('A')

65

>>> chr(66)

'B'

 

Quotes to create a string
we can create strings by single or double quotes. E.g:

a = 'abc'; the benefits of b = "sxt" use two quotes that can create a string itself contains quotation marks, without the use of an escape character. E.g:

>>> a = "I'm a teacher!"

>>> print(a)

I'm a teacher!

>>> b = 'my_name is "TOM"'

>>> print(b)

my_name is "TOM"
three consecutive three double quotes or single quotes, you can help us create multi-line strings. E.g:

>>> resume = ''' name="gaoqi"

company="sxt" age=18

lover="Tom"'''

>>> print(resume)

name="gaoqi"

company="sxt" age=18

lover="Tom"

 

len () function

Python allows there is an empty string, does not contain any characters and a length of 0. For example: >>> C = '' >>> len (C) 0
len () is used to calculate how much the character string contained. E.g:

>>> d = 'abcxsw'

>>> len(d)

6

Escape character
we can use "\ + special character", it is difficult to achieve certain effects represented by the characters. For example: line breaks and so on. Common escape characters that have these: Escape Character Description

\ (When end of line) ULink

\\ backslash

\' apostrophe

\" Double quotes

\ B Backspace (Backspace)

\ N newline

\ T horizontal tab

\ r carriage
>>> a = 'I \ nlove \ nU'

>>> a

'I \ nlove \ HDU

' >>> print(a)

I love U

>>> print('aaabb\ cccddd')

aaabbcccddd

 

Copy the string
using the string copy * can be achieved.

>>> a = 'Sxt'*3

>>> a

'SxtSxtSxt'

Not a new line
we call the front print, will automatically print a newline. Sometimes, we do not want to wrap, do not want to automatically add a line break. We can own the parameter end = "any string." Add anything to achieve the end:

print("sxt",end=' ')

print("sxt",end='##')

print("sxt")

Operating results are

sxt sxt##sxt

Other related methods

 

a.startswith () begin with the specified string

 a.endswith () ends with the specified string 
a.find () the position of the first occurrence of the specified string

 a.rfind () last occurrence of the specified string position

 a.count () specified string a few times

a.isalnum () all the characters are all letters or numbers

 strip () specifying information end to end string is removed.

 the lstrip () removing the left of the string specifying information

a.capitalize () generates a new string, first letter capitalized

a.title () generates a new string, each word capitalized

a.upper () generates a new string, full speed all the characters to uppercase

a.lower () generates a new string, all characters to lower case full

a.swapcase () to generate new, all letter case change

 

isalnum () is a letter or number

Whether the isalpha () detects only the alphabet character string (including Kanji).

 Whether isdigit () detects only the string of digits.

 isspace becomes () detects whether whitespace

isupper () is an uppercase letter

 islower () is a lowercase letter

 

Guess you like

Origin www.cnblogs.com/1208xu/p/11550024.html