python There are many ways to intercept and replace strings

Time :2016-03-12 20:08:14 source : The Internet
Reading guide :python There are many ways to intercept and replace strings , Colon separated string interception method ,python String substitution method , In the way of the string itself , Or replace the string with regular .
from :
python There are many ways to intercept and replace strings

One , String truncation
Python Is an ordered set of strings , You can use the index to extract the characters you want to get , hold python As a list of strings, it's easier to understand .

python The list of strings for has 2 The order of values is :
The first is the left to right index 0 At the beginning , The maximum range is less string length 1:

Copy code example :
s = 'ilovepython'
s[0] The result is i

The second is the right to left index -1 At the beginning , The maximum range is at the beginning of the string :

Copy code example :
s = 'ilovepython'
s[-1] The result is n

This is to get a character , If you want to get a break , You can use python String interception function , such as :

Copy code example :
s = 'ilovepython'
s[1:5] The result is love

When using colon delimited strings ,python Return a new object , The result contains consecutive content identified by this pair of offsets , The start on the left contains the lower boundary , For example, the results above include s[1] Value l, The maximum range taken does not include the upper boundary , Namely s[5] Value p.
Give more examples :
The result is 'ilo', Omit : On the left , Silently, the index value from the left is 0, In fact, this expression is similar to s[0:3] It means the same .

The result is :'ilovepytho' That is, the index value starting on the left is 0, The maximum range is to the last character in the string, but does not include the last character .
The result is :'ilovepython' Got the offset from 0 Elements between the end , This is a python String copy .

Two ,python String substitution

python String substitution method .

Two ways to do python String substitution :

1、 In the way of the string itself replace Replace string .

2、 Replace string with regular .

Example :

Copy code example :
a = 'hello word'

hold a In a string word Replace with python.

1、 Using the string itself replace Method

Copy code example :

Output :
hello python

2、 Replace strings with regular expressions

Copy code example :
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b

Output :
hello python

  Related articles

python String interception and replacement examples of more related articles

  1. python String interception

    We can use the index to extract the characters we want to get , You can put python It's better to understand the string as a list of strings python The list of strings for has 2 The order of values is 1 Is the left to right index by default 0 At the beginning , The maximum range is less string length 1s = 'ilo ...

  2. python String interception

    python String interception str = 'abcd' str[0:-1],-1 The beginning of the end , however [] The operator does not take the character corresponding to the suffix : therefore str[0:-1] ---->‘abc' str[0:1 ...

  3. python String interception 、 lookup 、 Division

    Python Intercepting strings using Variable [ Header subscript : Tail subscript ], You can intercept the corresponding string , The subscript is from 0 From the beginning , It can be positive or negative , The subscript can be empty to indicate that the head or tail is taken . # example 1: String interception str = '1234567 ...

  4. mysql String truncation and replacement in

    -- Replace replace( Field name ," Characters that need to be replaced "," Replaced characters ") mysql in replace Regular matching is not supported mysql> set @needRep ...

  5. String Classic cases of string interception and replacement

    Share today's interview question ! It's not difficult , But it didn't work out subject : take String  str=" full name : Wu Yawei , Age :27, Address : Xi'an City ": The output is : full name = Wu Yawei Age =27 Address = Xi'an City ...

  6. python—— String interception

    str = ‘0123456789’ print str[0:3] # Intercept the first to third characters print str[:] # Intercepts all characters of a string print str[6:] # Intercept the seventh character to the end p ...

  7. Python String specifies the position to replace the character

    Replace character at specified position def replace_char(old_string, char, index): ''' String replaces characters by index position ''' old_string = str(old_string ...

  8. python String content replacement method ( Reprint )

    python String content replacement method Time :2016-03-10 06:30:46 source : The Internet Reading guide :python String content replacement method , Including single character substitution , Use re Regular matching for string pattern search and replacement method .   ...

  9. Python The principle of string interception , The location of the subscript

    Python There's always a bit of confusion when it comes to string interception , Find a diagram on the official website , understand Python How strings are marked , The specific meaning of is illustrated as follows : +---+---+---+---+---+---+ | P | y | t | h ...

Random recommendation

  1. Java Collection Overview

    Containers , It's used to hold things , stay Java in , Things are objects , And loading objects is not putting real objects in , It's about saving references to objects . Pay attention to object references and object relationships , The following example illustrates the relationship between objects and object references . String str = ...

  2. Android Native game development : Use JustWeEngine Develop wechat and fly

    Use JustWeEngine Develop wechat and fly : The author blog : Blog Garden Engine address :JustWeEngine Sample code :EngineDemo JustWeEngine? JustWeEngine It's hosted in Git ...

  3. oracle Novice solutions to common problems

    There may be many reasons for the following problems , But as far as Xiaobai is concerned , I only record all the problems I encountered in my study . Hope for some novices Xiaobai people help . as a result of sys No sysdba user , You're going to take it as sysdba The user login ...

  4. filter_map

    #!/usr/bin/env python # -*- coding:utf-8 -*- ret = filter( lambda x: x < 3, [1, 2, 3, 4, 5]) prin ...

  5. Delete... From the right-click menu &ldquo; Use Aliwangwang to send this file &rdquo; term

    Enter... In the input box in the run dialog box Regedit.exe, Click the OK button to start the registry editor program . Expand on the left side of the registry editor window HKEY_CLASSES_ROOT\CLSID{0DE1378D-F811-40E ...

  6. ( primary ) call jpeglib Compress the image

    website : Without permission , It is strictly prohibited to reprint . Reference website : ...

  7. hdu1753() Analog large live digital add

    Heading information : Manually large real numbers together , pid=1753"> AC Code : /**  * Big real numbers add up ...

  8. QUIC Briefly

    QUIC. namely Quick UDP Internet Connection, Be similar to SPDY, The same is also caused by Google The company has extended the existing protocol , And to reduce network latency . I've introduced before SPDY Information about ...

  9. java Annotation generation xml And contains CDATA problem

    Baidu java Generate xml, There's a big push , The main way to generate is to use Dom4J , There is another use Jdk Own annotation class ! The following is mainly about the use of my annotation class ,( You can refer to this article Dom4J Generate xml And contains CDATA problem ) and x ...

  10. Use AForge.NET Framework Turn on the camera and take a screenshot C#

    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == ...