Python string tricks from Python Cookbook

Source: Internet
Author: User
Tags ord

All data into the program is just a bunch of bytes

English character takes up one byte Chinese is two bytes one byte byte=8bit

Unicode strings set a uniform and unique binary encoding for each character in each language

Big = R ' This is a \tsting ' R denotes the original, what is inside the string is what, the backslash is a backslash, and will not escape

S.isdigit () determines whether a string is a number

Ord and CHR characters and conversions that correspond to the value of the Andean Code

Ord (a)

Chr (97)

Determine if the object is a string:

def isstringlike (anobj):

Try:anobj + "

Except:return False

Else:return True

String alignment:

S.ljust (20)

S.rjust (20)

S.center (20)

S.center (20, ' + ') is not filled with spaces, but with a plus sign

Strip both ends of the string:

X.lstrip ()

X.rstrip ()

X.strip ()

S.strip (' xy ') can also specify what to remove

Python built-in functions:

Filter (fun, seq)

Map (fun, seq)

Reduce (fun, seq) sequentially iterate

Reduce (fun, seq, starting value)

Lambda X, Y:x+y

Operator module

Import operator

Operator.add

..... subtraction, etc...

Help (operator)

Verbatim reversal of strings:

S[::-1]

Reverse the word only:

Import re

Rev = Re.split (R ' (\s+) ', ' Hello World hahaha! ‘)

Rev.reverse ()

Rev = '. Join (REV) #要用空字符来join because the space is already in the cut list

Returns all elements in a that are not part of B:

Set (a). Difference (set (b))

Translate method:

Import string

s = ' Hello world! '

Table = String.maketrans (' abcde ', ' 12345 ') #先生成对照表

S.translate (table) #依照对照表进行替换操作

S.translate (table, ' wor ') #替换后, delete characters containing wor

If you want to delete only, set the table to: Table = String.maketrans (","), which will only do the delete operation.

3/4 #0

From __future__ Import Division

3/4 #0.75

4//4 #0

Capitalization issues:

S.upper () All Caps

S.lower () All lowercase

S.capitalize () First uppercase, remaining lowercase

S.tittle () capitalize the first letter of each word, the remaining lowercase

S.split ()

S.split (' \ n ')

S.splitlines ()

S.splitlines (True) preserves end-of-line newline characters

Python string tricks from Python Cookbook

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.