Cloud Classroom-python Learning notes (5)

Source: Internet
Author: User

  • String definitions:
    • A string is a sequence of characters
    • Surround with paired single or double quotes
      • One_quote_str = ' Hello world! '
      • Two_quote_str = "Hello world!"
    • ? or three quotation marks ("" "or")
      Preserves all formatting information in a string

      • "This is
      • A test
      • Today ' "
  • Basic string Operations
    • Length (len () function)
      1 ' python ' 2 print len (first_name)
      Output: 6
    • Stitching (+)
      1 ' python ' 2 ' Testing ' 3 Print Name
      Output: pythontesting
    • Repeat (*)
      1 ' python ' 2 Print name * 3
  • Member operator (in)
    • Determines whether a string is a substring of another string, returns a value of TRUE or False
      1Name ='python'2 Print 'x' inchname//false3 Print ' on' inchname//true4 Print ' on' inchName//false, case sensitive
  • For statement, each character in the enumeration string
    ' python '  for inch Name:     Print CPython
  • example, count the number of vowels in a string
    1 defVowles_count (s):2Count =03      forCinchS:4         ifCinch 'Aeiouaeiou':5Count + = 16     returnCount7 8 PrintVowles_count ('Hello world!')
  • String index (indexed)
    • Each character in the string is indexed with an index value (subscript)
    • Index starts at 0 (forward) or-1 (backwards)
      1str ='Hello World'2 3 PrintStr[2]4 PrintStr[-3]5 PrintStr[15]6 7 L8 R9 Traceback (most recent):TenFile"<stdin>", Line 1,inch<module> OneFile"C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", Line 540,inchRunfile A execfile (filename, namespace) -File"c:/users/weidan/desktop/untitled0.py", Line 12,inch<module> -     PrintStr[15] theIndexerror:string index out of range
  • Slicing (slicing)
    • Select a subsequence of a string
    • Grammar [Start:finish]
      • Start: The position at which the sub-sequence begins
      • Finish: The index value of the next character in the ending position of the subsequence
      • if start or finish is not provided, the default start is the first character, and finish is the last character
          str =  '  
  • Count parameters
    • Receive 3 parameters
      • [Start:finish:countBy]
      • The default countby is 1
    • Get inverse string
      1 ' Hello World ' 2 3 print str[2:6:2]4print str[::-1]//Get inverse string 5Print str[:6:2]67lo8dlrow Olleh9 Hlo
  • The string cannot be changed.
    • Once generated, the content cannot be changed
      1str ='Hello World'2STR[1] ='a'3 PrintStr4 5 Traceback (most recent):6File"<stdin>", Line 1,inch<module>7File"C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", Line 540,inchRunfile8 execfile (filename, namespace)9File"c:/users/***/desktop/untitled0.py", Line 9,inch<module>TenSTR[1] ='a' OneTypeError:'Str'Object does notSupport Item Assignment A>>>
    • Generate a new string by slicing, etc.
      1 ' Hello World ' 2 ' a ' + str[2:]3print  str45 Hallo World  

Cloud Classroom-python Learning notes (5)

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.