Python Learning string

Source: Internet
Author: User

Recently in the study of Python, easy to make some records, to facilitate later review

#字符串是不可再改变的序列
Aa= ' ABCD '
#aa [2:]= ' FF ' #报错, not directly assignable
#字符串格式化: Use the format operator, or percent%, to implement
print ' Price of Aggs: $%d '%42
mm= ' Hello '
Nn= ' World '
Print '%s go%s '% (MM,NN)
#String template string Formatting: Replace the $foo in the string with the passed keyword argument (foo)
S=template (' $x is $x ')
Print S.substitute (x= "Lyq")
#字段宽度: The minimum number of characters retained by the converted value
print '%10f '%pi
#字段精度: The number of decimal digits that should be included in the result, or (for string conversions) the maximum number of characters that the converted value can contain
print '%.2f '%pi


# 1.find method can find a substring in a longer string, which returns a substring sitting at the leftmost index of the position, and returns 1 if not found
Parent= "I always stand here"
Print Parent.find ("Al")
Print Parent.find ("KK")
Print Parent.find (' A ', 6,15) #寻找起点和终点
#2. Split: Splitting a string into a sequence
Print Parent.split ("")
Print Parent.split ("", 2) #按 "" Split 2 times
Print Parent
#3. The join method is the inverse of the split method, which is used to add elements to the queue
seq=[' 1 ', ' 2 ', ' 3 ', ' 4 ']
sep= ' + '
Print Sep.join (seq)
#4. Lower: Returns the lowercase master of a string
Aa= "I like Here"
Print Aa.lower ()
#5. Replace returns a string after all occurrences of a string have been replaced
bb= ' E://DFE//DFD '
Print Bb.replace ("//", '/')
#6. Strip: Remove the spaces on either side of the string or specify the characters that need to be removed
Cc= "Can do it!! "
Print Cc.strip ()
cc= "***********you **can do it!! ******"
Print Cc.strip ("*!")
#7. Translate: As with the Replace method, you can replace portions of a string, only single characters, and multiple conversions at the same time, with the parameter table type
#maketrans函数接受两个参数: Two equal-length strings, with each character in the first string replaced with a character in the same position in the second string
Table=maketrans (' CS ', ' 23 ')
Print "This CA CS JJ". Translate (table)

Python Learning string

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.