Python string base operation

Source: Internet
Author: User

============== string ========

>>> s1= ' www.baidu.com '

>>> type (S1)

<type ' str ' >

>>> Type (2)

<type ' int ' >

>>> type ("' DSFDSJK ')

<type ' str ' >

with type () check what type it is .

Access to Strings s1[0]= ' S1[2]=w use the subscript to access

>>> s6= ' AA\NBB '

>>> Print S6

Aa

Bb

>>> #/n is an escape character that represents a variety of newline characters in the line break record

>>> s6= ' AA\NBB '

>>> Print S6

Aa

Bb

>>> s7=r ' aa\nbb ' # when Plus R the time is not escaping . "Close the escape mechanism"

>>> Print S7

Aa\nbb

>>> s8=u ' aa\nbb ' # when Plus u the time indicates that the string followed is Unicode Coding

>>> Print S8

Aa

Bb

>>> '%10s%10d%10.2f '% (' record ', 21,25.652) formatted string %s formatted as String %d formatted as an integer %f formatted as a floating-point number ( Anyway, there are several format placeholders on the side of the corresponding values separated by commas ) 10.2f represents floating-point number accounted for Ten bit after decimal point reserved 2 bit Rounding

>>> print ' name%s age%d record%f '% (' amily ', 25,99.9)

Name amily Age Record 99.900000

>>> s1= ' Www.baidu '

>>> s2= '. com '

>>> Print S1+s2 # + Plus String Connection   + must be a string type before and after

Www.baidu.com

>>> s1= ' Hello amily '

>>> s2=2

>>> print S1+str (S2) converted to string type and then add operation

Hello Amily2

>>> li1=[1]*5 #* Asterisk string repetition equals N addition * before the string * is a number

>>> Print Li1

[1, 1, 1, 1, 1]

>>> li2=[' AA ']*5

>>> Print Li2

[' AA ', ' AA ', ' AA ', ' AA ', ' AA ']

>>> s1= ' 111 ' *20

>>> print S1

111111111111111111111111111111111111111111111111111111111111

>>> s1= ' www.baidu.com '

>>> Print S1[4] # through [] subscript accesses the value of the first position of the string Str[index]

B

>>> s1= ' www.baidu.com '

>>> print s1[-4] #负数也可以, negative numbers are starting from the right.

.

>>>

>>> Print S1[2:4] # Str[start:end] slices Start Slice Start End Slice end point The starting point or end point can not be filled Writing negative numbers starts from the right .

W.

>>> #这里切片起点必须小于终点 because it is cut from left to right.

If you want to slice from right to left, then str[big:small:-1], this is the reverse output

>>> s1= ' www.baidu.com '

>>> Print S1[8:3:-1]

Udiab

>>> s1= ' www.baidu.com '

>>> print s1[-1::-1] # implement reverse output of strings

Moc.udiab.www

>>>

string Other common functions     

>>> s= ' DAFDFH234234HJK '

>>> s.isalnum () #str. Isalnum Determines whether a string is a number or a letter

True

>>> s= ' SADFHD.? <dfah '

>>> S.isalnum ()

False

>>>

>>> s= ' sdfhdjshf0 '

>>> S.isalpha () #str. Isalpha Determine if all letters are in a string

False

>>> s= ' DFDFHDGF '

>>> S.isalpha ()

True

>>> s= ' 132746 '

>>> s.isdigit () #str. IsDigit Determine if all numbers are in a string

True

>>> s= ' sdfgydh678 '

>>> S.isdigit ()

False

>>> s= ' ABV '

>>> s.isupper () #str. Isupper Determine if all uppercase letters are in a string

True

>>> s= ' Dfhu '

>>> s.islower () #str. Islower Determine if all lowercase letters are in a string

True

>>> s= '

>>> s.isspace () #str. Isspace to determine if a string is a space

False

>>> s= '

>>> S.isspace ()

True

>>> s= ' DJFDJSFH '

>>> s.upper () #str. Upper string converted to uppercase

' DJFDJSFH '

>>> s= ' DJKJSDJFK '

>>> s.lower () #str. Lower strings converted to lowercase letters

' DJKJSDJFK '

>>> s= ' DJKJSDJFK '

>>> S.lower ()

' DJKJSDJFK '

>>> s= ' Dfhajkh '

>>> S.upper ()

' Dfhajkh '

>>> S.lower ()

' Dfhajkh '

>>> s= ' DSJFHDSJKF kdjfhj '

>>> S.strip () #str. Strip is one or more spaces (generalized spaces, \n,\t) that remove the left or right of a string

can be written as str.strip (' \ n ')

#str. Split () the space between the front and back can be removed

' Dsjfhdsjkf kdjfhj '

>>> S.rstrip () #str. Rstrip is the space to remove the right side of the string (multiple spaces can be removed)

' Dsjfhdsjkf kdjfhj '

>>> S.lstrip () #str. Lstrip is the space to remove the left side of the string (multiple spaces can be removed)

' Dsjfhdsjkf kdjfhj '

#coding: Utf-8

s= ' www.baidu.com '

s1= ' www '

s2= '. com '

If S.startswith (S1): #str. StartsWith (STR1) determines whether a string starts with str1 and returns a Boolean value

print ' www '

If S.endswith (S2): #str. EndsWith (STR1) determines whether the string ends with STR1, returns a Boolean value

print '. com '

>>> s= ' www.baidu.com '

>>> ID (s) #id (str) to view the address of the string in memory

46351608

>>> s1=s.replace (' B ', ' B ') #str.replace (' A ', ' B ') to replace a in the string Change to b

>>> ID (S1)

46353008

>>> s=s.replace (' W ', ' s ') #可以看到replace函数 "modified" the original string, the position of the string changed, the description is actually not the original string, so the string is not allowed to be modified , it just produced a new string.

>>> ID (s)

46353808

>>> Print S

SSS.baidu.com

>>> s= ' dsfdsf f d 234h djfha '

>>> li=s.split () #str. Split () string Split to return a list type

>>> Print Li

[' DSFDSF ', ' f ', ' d ', ' 234h ', ' DJFHA ']

This actually can be achieved directly with split, but the most important thing here is the gradual progression of ideas. Write a more complex algorithm must have a gradual thinking, slowly, a little to achieve, and then in combination, especially the cycle of this one can not find the law, it is possible to gradually walk several loops, find the regular and then write while or for statement.

#coding: Utf-8

def mysplit (s):

S=s.strip ()

while (S.find (")!=-1):

Index1=s.find (")

S1=S[:INDEX1]

S=s[len (S1):]

S=s.lstrip ()

Print S1

Else

Print S

Mysplit (' dsfdsf f D 234h djfha hfkdjfdjd dkfdk dkfjadkjf dfajkdf adkfasdfhjd 34737 ')

Str.split () The detail record split () returns the list type of STH as a delimiter, dividing N times (divided into n+1 parts), taking the first several. Split (' m ', n) [x]

>>> u = "www.doiido.com.cn"

#使用默认分隔符

>>> Print U.split ()

[' www.doiido.com.cn ']

#以 "." As a delimiter

>>> print U.split ('. ')

[' www ', ' doiido ', ' com ', ' CN ']

#分割0次

>>> print U.split ('. ', 0)

[' www.doiido.com.cn ']

#分割一次

>>> print U.split ('. ', 1)

[' www ', ' doiido.com.cn ']

#分割两次

>>> print U.split ('. ', 2)

[' www ', ' doiido ', ' com.cn ']

#分割两次, and take a sequence of 1 items

>>> print U.split ('. ', 2) [1]

Doiido

#分割最多次 (actual with no num parameter)

>>> print U.split ('. ',-1)

[' www ', ' doiido ', ' com ', ' CN ']

#分割两次 and save the divided three sections to three files

>>> u1,u2,u3 = U.split ('. ', 2)

>>> Print U1

Www

>>> Print U2

Doiido

>>> Print U3

com.cn

Remove line break

>>> C = "' Say

Hello

Baby ""

>>> Print C

Say

Hello

Baby

>>> print c.split (' \ n ')

[' Say ', ' hello ', ' baby ']

Detach file name and path

>>> os.path.split ('/dodo/soft/python ')

('/dodo/soft ', ' python ')

>>> os.path.split ('/dodo/soft/python/')

('/dodo/soft/python ', ')

good example

>>> str= ' Hello hahaha<[www.baidu.com]>gdf238728 '

>>> print Str.split (' [', 1) [1].split ('] ', 1) [0]

Www.baidu.com

>>> print Str.split (' [', 1) [1].split ('] ', 1) [0].split ('. ')

[' www ', ' Baidu ', ' com ']

>>>

Python string base operation

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.