Python strings and basic operation (entry must see)! !

         Getting knowledge base has not finished updating, today Zhang then bring common operating entry-level string to everyone. This article is for white just getting started, please bypass the bigwigs.

First, the definition

String means "string of characters", such as "Hello, Charlie" is a string, "How are you?" Is a string.
Python requires string must be enclosed in quotes, also use single quotes row, double quotation marks, as long as both sides of the pair of quotation marks can.
Simply under the following characteristics:

  • 1. Character: that single text symbols,

  • 2. String: i.e. is ordered sequence of characters, wherein a ',', '' ' "" ", the contents enclosed

  • 3. Index: a row of numbers reflect the position of a character, the table is indexed from 0 using brackets [] to obtain the data, then the reverse start from -1
    , for example:

s = "Leslie Cheung, Andy Lau handsome but more" 
Print (S [-4] + S [2] + S [1] + S [0]) 
# results 
Zhang Hua Liu De

  

 

Second, common operations

Remember, strings are immutable objects, so any operation will not have any impact on the original string, it returns a value

  • 1.upper () to uppercase, lowercase is ignored when using the corresponding is lower ()

 
  • 2.strip () is removed when the left and right ends of the blank, including the brackets and \ n and \ T, user inputted content for the blank

 
  • 3.replace ( "old", "new") String replace

 
  • 4.split ( "to the cut", the number of times) is the result of cutting the string list (list), the default blank cutting

 
  • 5.startswith ( "which begin with") to determine whether to begin with endswith xxx is the corresponding output is False and True

 
  • 6. find () to find the location variable appears, showing the index, if the finding is -1, if the index is to find, if not the program will complain

 
  • 7. isdigit () determines whether numbers (integers) result is False and True Similarly isupper (determines whether capitalized) islower (determined whether lowercase) .isalpha (whether or letter)

 
  • 8. len () required length. Built-in functions

 
  • 9.count ( "") count

 
  • 10.center () center

 
  • 11.rjust () right justified

  • 12.ljust () Left

 
  • 13. iterative
    format:

for 变量 in 可迭代对象:    
   代码块      #可迭代对象中的每一个元素,分别赋给前面的变量,,可以将迭代对象遍历.
 
  • 14 slices
    Format:
    srt [Start: end: step]
    start capturing from the start, end to end, but does not include end, step do not write, then the default is 1 and cut from left to right, positive step from left to right, if it is negative, from right to left, to take an example, every n:

 
  • 15. The use of chips to determine whether the case is a palindrome columns:
    detailed code scanning announcement two-dimensional code, public concern number - Getting Started Tutorial - The Basics be acquired

Guess you like

Origin www.cnblogs.com/xiaozhangpython/p/12592027.html