Python string, List function

Source: Internet
Author: User

First, the string function


1, Capitaliza first letter capital
# name = ' Alex '
# v = name.capitalize ()
# Print (v)
#
2, all caps are lowercase, casefold can be transferred to many languages, lower can only be transferred to English.
# name = ' AleX '
# v = name.casefold ()
# Print (v)
# name = ' AleX '
# v = name.lower ()
# Print (v)
#
3, center string Center, Parameter 1 (required) is the total length, parameter 2 (optional), default blank, fill character (only one)
# name = ' Alex '
# v = name.center (20, ' 2 ')
# Print (v)
#
4. The number of occurrences of the passed in string
# name = ' Alexal '
# v = name.count (' a ')
# Print (v)
#
5. Convert to bytes
# name = ' Zhang Yi '
# v = name.encode (encoding= ' utf-8 ')
# Print (v)
#
#
#
6. Whether to end with incoming value
# name = ' Alex '
# v = name.endswith (' x ')
# Print (v)
#
7. Whether to start with the incoming value
# name = ' Alex '
# v = name.startswith (' a ')
# Print (v)
#
8. Replace tab and newline character \ n (contains front character), incoming value is replacement length, display table format
# name = "ALEX\TERIC\TMERRY\NA\TB\TC"
# v = name.expandtabs (20)
# print (v)
#
9. Find the index position of the passed-in value in the string, there is no return-1
# name = ' Alex '
# v = name.find (' a ')
# Print (v)
#
10. String formatting
# # corresponding to the index location
# info = "I am: {0}; Age: {1}; Gender: {2}"
# v = Info.format ("Zhang Yi", 22, ' female ')
# print (v)
# # corresponding to the name
# info = "I am: {name}; Age: {Ages}; Gender: {Gender}"
# v = Info.format (name= "Zhang Yi", age=22,gender= ' female ')
# Print (v)
#
11, string formatting, according to the dictionary corresponding
# info = "I am: {name}; Age: {Ages}; Gender: {Gender}"
# v = info.format_map ({' name ': "Zhang Yi", ' age ': $, ' gender ': ' Female '})
# Print (v)
#
12. Find the index position of the passed-in value in the string, there will be an error
# name = ' Alex '
# v = name.index (' a ')
# Print (v)
#
13, whether it is a number, Chinese characters, true false
# name = ' Merry1 Zhang Yi '
# v = name.isalnum () #true words, numbers, Chinese characters
# v2 = Name.isalpha () #false Word, Kanji
# Print (v2)
#
#
14. Judging whether it is a number,
# num = ' ② '
# v1 = num.isdecimal () # ' 123 '
# v2 = num.isdigit () # ' 123 ', ' ② '
# v3 = num.isnumeric () # ' 123 ', ' two ', ' ② '
# Print (V1,V2,V3)
#
15. Whether a string in a variable conforms to a rule (contains numbers, letters, underscores, and cannot start with a number, but cannot judge a keyword)
# name = ' Alex '
# v = name.isidentifier ()
# Print (v)
#
16. Are all lowercase
# name = ' Alex '
# v = name.islower ()
# Print (v)
#
17, whether all uppercase
# name = ' ALEX '
# v = name.isupper ()
# Print (v)
#
18. Contains the implied \ t \ n, etc.
# NAME = "It's a nice day today, \ t let's go play together!" "
# v = name.isprintable ()
# Print (v)
#
19, whether all the space
# name = ' '
# v = name.isspace ()
# Print (v)
#
20, whether is the title format, each English word first letter is capitalized
# t1 = ' Can you Say Again? '
# v = t1.istitle ()
# Print (v)
#
21. Element concatenation (element String) * * * * * * ()
# name = ' Alex '
# v = "_". Join (name) # inner Loop each element
# Print (v)
# name_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ']
# v = "porridge". Join (Name_list)
# print (v)
#

Second, list function

1. Append
# user_list.append (' Zhu Yi ')
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# Print (user_list)
#
2. Clear
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# user_list.clear ()
# Print (user_list)
#
3. Copy
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# v = user_list.copy ()
# Print (user_list)
# Print (v)
#
4. Counting
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# v = user_list.count (' Zhang Yi ')
# Print (v)
#
5. Extensions (Append list)
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# user_list.extend ([' Zhu Yi ', ' Chu er ')
# Print (user_list)
#
6. Index (look for index position by value, from left to right, show only first one)
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# v = user_list.index (' Zhang Yi ')
# Print (v)
#
7. Insert (parameter one is the position index, parameter two is the insertion value)
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# user_list.insert (0, ' Zhu Yi ')
# Print (user_list)
#
8. Delete (by index)
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# user_list.pop (0)
# Print (user_list)
#
9. Delete (delete by value, delete only first from left)
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# user_list.remove (' Zhang Yi ')
# Print (user_list)
#
10. Flip
# user_list = [' Zhang Yi ', ' Zhang Yi ', ' Zhang Yi ', ' Zhang San ', ' Zhang Si ', ' Zhang Five ', ' Zhang Six ']
# User_list.reverse ()
# Print (user_list)
#
11. Sorting
# user_list = [' 1 ', ' 3 ', ' 8 ', ' 5 ']
# User_list.sort ()
# Print (user_list)


This article is from the "ermei22" blog, make sure to keep this source http://ermei22.blog.51cto.com/9186534/1925243

Python string, List function

Related Article

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.