Python note 9-string manipulation

Source: Internet
Author: User

1. Defining strings

The string has a subscript starting with 0, and [] can be used to remove the label

2. String reading

The string can use loop for, one at a time to take out each element of the Loop object

3. String modification-string not modifiable

Strings are immutable variables and cannot be modified once defined

Name= "123123213"

Name[2]=7 This change will be an error

4. String manipulation

name = ' HeLLo World ' #定义字符串
Print (name[0]) #读取字符串元素

(1) Character element manipulation

# Print (Name.capitalize ()) # Capitalize first letter
# Print (Name.center (50, ' * ')) # 50 *, put name in the middle
# Print (Name.endswith (' d ')) # whether to: End, determine if D is the end, return True or False
# Print (Name.find (' sss '))#查找字符串的索引, find the words to return the index subscript that appears for the first time, cannot find return-1
# Print (Name.format (name= ' Niuniu ', age=18)) # This is the format string
# Print (Name.format_map ({' name ': ' Niuniu ', ' age ': 19}) # This is also a formatted string followed by a dictionary
# print (' abA123 '. Isalnum ()) # To determine if it contains numbers and letters, return T F
# print (' AbA '. Isalpha ()) # to determine if it is an English letter, return T F
# Print (' 122 '. IsDigit ()) # To determine if it is a number, return T F
# print (' 11sdf '. Isidentifier ()) # is a valid variable name
# print (' AA '. Islower ()) # to determine if it is a lowercase letter
# print (' AA '. Isupper ()) # to determine if it is a capital letter
# Print (Name.lower ()) # turns lowercase
# Print (Name.upper ()) # becomes uppercase

(2) Stitching string-join method
names = [' Zcl ', ' py ', ' zyz ']
strnames = ' ABCEDF '
Print (' * '. Join (strnames))#用 * Number to link strnames string, a*b*c*e*d*f
# Print (' strname: ', strnames)
# join is used to stitch each element of an iterative object through a string .
#所有字符串的方法, it will not modify the value of the original string, it will produce a new string


(3) Go to string whitespace method

# print (' wubing\nhouning ')   #\n is a newline character and cannot be removed in the middle
# print (' \nmysql \ n '. Lstrip ()) # By default, the left space and line breaks are removed
# print (' \nmysql \ n '. Rstrip ()) # Default to remove the right space and line breaks
print (' \nmysql \ n '. Strip ()) # Default to remove both sides of the space and line wrapping
print (' Mysqlm '. Strip (' m '))#当你传入某个字符串的话, it will only remove the string you specified
#返回ysql 2-side m was removed, and the space and line-wrapping went away.

(4) String mapping

# p = Str.maketrans (' ABCDEFG ', ' 1234567 ') # precedes the string and follows the string to do the mapping
# print (' Ccaegg '. Translate (P)) # Output follows the above Maketrans-mapped string output: 331577
# new_p = Str.maketrans (' 1234567 ', ' ABCDEFG ')
# print (' Ccaegg '. Translate (new_p))

(5) Replacement string

st= ' MySQL is db. MySQL mysql-MySQL '
# Print (st.replace (' MySQL ', ' Oracle ', 1)) # Replacement string, first 1 string replaced, 1 not written replace All


# print (' MySQL is a db '. RFind (' is ') # Returns the subscript of the rightmost character

(6) Separating string-split method

name1 = ' Zcl,py,zyz ' #定义字符串
New_name = [] #定义列表
#1, have a list to save it first
Comma-separated values #2 and loops

# Print (Name1.split (', ')) #在字符串name1中, hit the comma and cut to the list. Input as: [' zcl ', ' py ', ' zyz ']
# Print (Name1.split ()) #按空格切割
#切割字符串, return a list, separate the string according to the string you specified, and put it in a list
#如果你什么都不写的话, separated by a space, multiple spaces are counted as a
# print (' 1+2+3+4 '. Split ()) # Cut string, return a list
# print (' 1+2+3\n1+2+3+4 '. Splitlines ()) # Split by line break
# print (' Abcdef '. Swapcase ()) # case Reversal

(7) methods to use after referencing the import string

name = ' HeLLo World '
Import String #先要引入

Print (string.ascii_letters+string.digits) #大小写字母 + digits
Print (string.ascii_lowercase) #所有的小写字母
Print (string.ascii_uppercase) #所有的大写字母
Print (string.digits) #所有的数字

5. Do not apply third-party variables, exchange the values of A and B

A = 0
B = 2
#不引入第三个变量
A = A+b
#a是3

B = a A
#b是1
A = a-B
#a是2
print (' ===== Here's what we're looking for ')
print (A, b)

#引入第三个变量的方法
# c = a
# #c是1
# a = b
# #a是2
# b = C
# Print (A, b)


#不让第三方变量, Exchange the values of A and B .
# A, B = b,a
# Print (' A: ', a)
# print (' B: ', b)
A, B = B,a
print (' A: ', a)
print (' B: ', b)

Python note 9-string manipulation

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.