2375 views|0 replies

4817

Posts

4

Resources
The OP
 

Python3 string substring search, replacement, deletion, splitting, connection related methods [Copy link]

 

Python3 string substring search, replacement, deletion, splitting, connection related methods

# Methods for searching, replacing, and deleting strings

#----- Find related methods----

s = 'crazyit.org is a good site'

print(s.startswith('crazyit')) #Determine whether the substring of s starts with crazyit and return True or False

print(s.endswith('site')) # Determine whether the substring of s ends with site and return True or False

print(s.find('org')) #Find the position where the substring org of s appears, and return -1 if it is not found

print(s.index('org')) #Find the position where the substring org of s appears. If it is not found, a ValueError error is returned.

print(s.find('org', 9)) #Start from index 9 to find out if org appears

#print(s.index('org', 9 )) #Start from index 9 to find out if org appears. If not found, a ValueError error is returned.

#---- Replace related methods ----

print(s.replace('it','xxxx')) #Replace all it in the string with xxxx

print(s.replace('it','xxxx', 1)) #Replace 1 it in the string with xxxx

#Define translation mapping table: {97 -> (a) : 945 -> (α) , 98 -> (b) : 946 -> (β) , 116 -> (t) : 964 -> (τ)}

table = {97: 945, 98: 946, 116: 964}

print(s.translate(table)) #Replace the string using the specified translation mapping table

The #str class provides a maketrans() method that can easily create a translation table.

table = str.maketrans('abc', '$@~')

print(table)

table = str.maketrans('abc', '357')

print(table)

# The program only needs to pass all the characters that need to be mapped as the first parameter of the maketrans() method.

# Use all mapped target characters as the second parameter of the maketrans() method.

st = 'crazyit.org is a good site'

table = str.maketrans('iot','***')

print(st.translate(table))

#--- Methods for deleting blanks and characters---

s = ' this is a puppy '

# Remove whitespace example

print(s.lstrip()) #Delete the blank space on the left side of the string

print(s.rstrip()) # Delete the blank space on the right side of the string

print(s.strip()) #Remove the spaces on both sides of the string

print(s) #When we print out s again, we see that s has not changed.

# Demonstrate the function of deleting the specified characters before and after the string

s2 = 'i think it is a scarecrow'

print(s2.lstrip('itow')) # delete the characters on the left

print(s2.rstrip('itow')) # delete the right character

print(s2.strip('itow')) # delete the left and right sides

#---- Split, connect related methods ----

# split() Splits a string into multiple phrases according to the specified delimiter.

# jojn() concatenates multiple phrases into a string.

# The following is a demonstration of usage:

s = 'crazyit.org is a good site'

print(s.split()) #Use blanks to split the string in s.

# The terminal prints the result of s: ['crazyit.org', 'is', 'a', 'good', 'site']

print(s.split(None, 2)) # Use whitespace to split the string, splitting at most the first two words.

#The terminal prints the result of s: ['crazyit.org', 'is', 'a good site']

print(s.split('.')) # Use a dot to split

# The terminal prints the result of s: ['crazyit', 'org is a good site']

mylist = s.split()

print('/'.join(mylist)) #Use '/' as a separator to join mylist into a string

# The terminal prints the result of mylist: crazyit.org/is/a/good/site

print(','.join(mylist)) #Use ',' as the separator to join mylist into a string

# The terminal prints the result of mylist: crazyit.org,is,a,good,site

# From the above running results, we can see that the split() and join() methods of str are inverse operations of each other.

 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list