Python string details

Source: Internet
Author: User
Tags integer join lowercase split

This article mainly introduces Python string details, this article explains the knowledge of string, some characters of string, raw string, Unicode string, common operation method of string, built-in function list and so on, need friends can refer to the following

Brief introduction

String sequences are used to represent and store text, and strings in Python are immutable, and once declared, they cannot be changed

Usually surrounded by single quotes ('), double quotes ("), Triple quotes ('" "")

Three of these quotes can be composed of multiple lines, a shortcut syntax for writing multi-line text, a common language file string, which is used as a comment at a specific location in the document. Easy Multiline Comment

Python actual three types of strings:

1. Usual meaning string (str)

2. Original string, starting with uppercase R or lowercase R, R ', no special characters are escaped

3.Unicode string, U ' basestring subclass

In Python, a string is an "immutable sequence."

1. Not variable

2. Meet the basic operation of the sequence, access by location, slice and index


1. Get help:

The code is as follows:

>>> Help (str)

>>> dir (str)

>>> Help (Str.replace)

2. Non-variability

After it is created, you cannot change in place (with Java), and you cannot change the characters into immutable sequences by assigning them to a location, which contains characters that are left to right and cannot be modified in situ. The string in Python corresponds to a list of immutable sequences, once declared, each character position is fixed

means that if you want to change, you must create a new one!

The code is as follows:

>>>s= ' spam '

>>>s[0]= ' K ' #TypeError

#修改字符串类似java, re-assign the value

s = ' k ' + s[1:]

Raw string

The original string constant, R "ABCD", (R/R) removes the backslash escape mechanism. Turn off the escape mechanism, that is, escape is no longer represented


1. Regular expressions

For handling regular expressions, reducing backslash

The code is as follows:

P4search = Re.compile (R ' s* ')

2. System path

can easily represent system paths

The code is as follows:

Path = R ' E:book '

Unicode string

Unicode is the standard way to write international text.

Python allows you to process Unicode text-you just prefix the string with u or U. For example, U "This is a Unicode string."

BP: Use a Unicode string when you're working on a text file, especially if you know that the file contains text written in a non-English language.

Common operations

1. Basic operation

The code is as follows:

+: String1+string2 #联接字符串, link the latter string to the back of the previous string

Python does not allow other types to appear in the + expression, it needs to be manually turned "this is different from Java" ' ABC ' +STR (9)

*: String*n #创建一个新字符串重复n次原来的串

[]: String[n] #从字符串中获取对应位置的一个字符

[:]: string[n:m] #截取字符串, if: M from head to M if N: from N to tail

In:char in string #判断一个字符是否在串中, if returned to True (true)

Not In:char not in string #判断一个字符是否不在串中, if returned as true (True)

R/R: r/rstring #禁止转义字符的实际意义, whole character as original meaning

Len (): Len Length (s)

2. Type conversion

Strings and numbers are converted to each other

String to Digital Int/float/long

Number to String str

The code is as follows:

>>> Int (42)


>>> Int (' 42 ')


>>> Str (42)

' 42 '

>>> float (' 42.0 ')


>>> Str (42.0)

' 42.0 '

Or a function that uses the string module

S: the string to be converted, base: optional, target system

The code is as follows:

Import string

String.atoi (S[,base]) #base默认为10, if 0, then s can be 012 or 0x23 This form of string, if 16 then s can only be 0x23 or 0x12 this form of string

String.atol (S[,base]) #转成long

String.atof (S[,base]) #转成float

Conversion of strings and lists

String goto list:

The code is as follows:

s= ' spam '

L = List (s)

L2 = "Hello World". Spilt ()

List-round string

Copy code code as follows:

k = '. Join (L)

Note that you cannot join a non-string in a list

3. Modify the string

The code is as follows:

s = s + ' a '

s = s[3:] + ' B '

s = s.replace (' pl ', ' pa ')

A = ' #赋值空

Del a #整个变量删除

4. Indexing and partitioning

Index S[i]

The code is as follows:

S[0] First

S[-1] = S[len (s)-1] reciprocal first

Fragmented S[i:j]

The code is as follows:

Excluding upper boundary, S[1:3] [1-2]

S[1:] Take 1 to end S[:3] take start to 2

S[:-1] start to the bottom of the second

S[:] Start to end, equivalent to a copy

S[1:10:2] Take 1-9, step =2

S[a:b:-2] Step is negative, two boundary meanings are reversed, representing from B+1 to a, step 2

s= ' ABCDEFG '

S[5:1:-1] Get Fedc

S[1:3] = = S[slice (1,3)] built-in functions

String formatting

This only introduces basic string formatting, extending in the following sections%c single character%d decimal integer%o Octal integer%s string%x hexadecimal integer, where the letter is lowercase%x hexadecimal integer, where the letter is capitalized

The code is as follows:

>>> str = "So%s a day!"

>>> str% ' beautiful '

' So beautiful a day! '

>>> ' {0} is {1} '. Format (' A ', ' B ')

' A is B '

>>> template = "{0}, {1} and {2}"

>>> Template.format (' A ', ' B ', ' C ')

' A, B and C '

Built-in function List

"String method is the largest tool for Python text processing"

String.capitalize ()

Capitalize the first character of a string (Width,[,fill])

The original character is centered and the space is padded to the width length

String.count (Str,beg=0,end=len (String))

Gets the number of substrings in a string, calculates the number of occurrences, and can specify a range

String.decode (encoding= ' UTF-8 ', errors= ' strict ')

Decode string, error default ValueError, unless errors is ignore or replace

String.encode (encoding= ' UTF-8 ', errors= ' strict ')

String.endswith (Suffix,beg=0,end=len (String))

Whether to end with * *

String.expandtabs (tabsize=8)

The tab in the string is converted to a space, and the default 8

String.find (Str,beg=0,end=len (STIRNG))

Detects whether STR is included, returns the start index, or returns-1

String.index (Str,begin=0,end=len (String))

With find, there is no newspaper anomaly, ValueError

String.isalnum ()

At least one character, and all characters are letters or numbers, True. Detects if a string contains only 0-9a-za-z

String.isalpha ()

At least one character, all characters are letters, True. Detects whether a string contains only letters

String.isdecimal ()

Contains only decimal digits, True

Stirng.isdigit ()

Contains only digits, True. Detects if a string contains only digits

String.islower ()

At least one case-sensitive character and all characters lowercase, True. Detect whether strings are all lowercase letters

String.isnumeric ()

Contains only numeric characters, True

String.isspace ()

Contains only spaces, True. Detects whether a string is a blank character

String.istitle ()

Title character, True. Detects if a word in a string is the capital letter

String.isupper ()

At least one case-sensitive character and all uppercase characters, True. Detects whether the string is all uppercase

String.Join (seq)

With string as the delimiter, all elements in SEQ are merged into a new string. Inserts the original string between each of the two characters in the parameter string

String.ljust (width)

Returns an original string left-aligned with space added to length width

String.Lower ()

Go to lowercase. Convert all strings to lowercase

String.lstrip ()

Cut off the left space

String.partition (str)

= Find+split, first position from STR, truncated to PRE_STR,STR,AFTER_STR tuple, excluding str pre_str=strstring.replace (Str1,str2,num=string.count ( STR1)) replaces, specifies no more than num times, and can be implemented as a template

String.rfind (Str,beg=0,end=len (String))

With Find, right start

String.rindex (Str,beg=0,end=len (String))

With index, right start

String.rjust (width)

Right alignment, padded space

String.rpartition (str)

With partition, start on the right.

String.rstrip ([chars])

Clears the right margin, including line breaks, and returns the processed string

String.Split (str= "", Maxsplit =string.count (str))

With str slices, you can specify the number of splits, split strings, return lists, default separator spaces

String.splitlines (Num=string.count (' n '))

S.splitlines ([keepends]) separated by rows to specify the number of splits

String.startswith (Obj,beg=0,end=len (String))

Start with STR, True. Detects whether a string begins with a substring

String.strip ([obj])

Performing Lstrip and Rstrip on a string


Reverses the case in string. String lowercase to uppercase, uppercase to small

String.title ()

Title flower, Word first letter uppercase, remaining lowercase

String.translate (str,del= "")

S.translate (table) converts a string character according to Str, and the character to be filtered is placed in the Del parameter

String.upper ()

Capitalize. Capitalize all strings

String.zfill (width)

Returns the string with length width, the original string is right-aligned, and the front padding 0

Len (String)

Gets the length of the string

Best practices

1. The length used in the cycle

The code is as follows:

While I < Len (stri):


size = Len (stri)

While I < size

2. String Append

Copy code code as follows:

L = [' A ', ' B ']

result = '

For I in L:

result = I


result = '. Join (L)


1. Escape character

Several commonly used:

N line-wrapping, back slash

T-Tab ' single quote

R return "Double quote

Subsequent need to expand

A detailed explanation of string coding

String formatting

Regular expressions

Strings involve common modules (serialization/text wrapping, etc.)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.