Python learning 2-using strings

Source: Internet
Author: User
Tags print format string format

String

All standard frontal sequence operations (index, Shard, multiply, Judge membership, seek length, maximum minimum) apply to the string, but the string is immutable

>>> a  = ' My name is Hahaahh ' >>> a[:3] = ' SDSD 'Traceback (most recent call last):  File "<p Yshell#24> ", line 1, inch <module>    a[:3] = ' SDSD 'TypeError: ' str ' object does not support item Assignme NT  

String Format Strings-Lite version

String formatting applies to the string formatting operator, which is percent% to implement

--formatted string%s

>>> format = ' Hello,%s,%s enough for you ' >>> values = (' haha ', ' Zhang ') >>> print format%
    
      Valueshello, Haha,zhang enough for 
     you

If you want to format a real number, use the%f

Formatted string--full version

Pass

String Method--to enumerate some particularly useful

Find-You can find substrings in a longer string, return the leftmost index where the substring is located, and return 1 if not found

>>> A = "Zhang is a good boy" >>> a.find ("is") 6>>> a.find ("Cheng") -1>>> " Zhang is a ". Find (" a ") 2  

Join--Used to concatenate elements in a sequence (elements must all be strings), which is the inverse method of Split

>>>SEQ = ["1", "2", "3"]>>> Sep = "+" >>> sep.join (seq) ' 1+2+3 ' 

Lower--The lowercase master that returns a string

Replace--Returns the string after all occurrences of a string have been replaced

>>> ' This was a boy '. Replace (' was ', ' KK ') ' Thkk kk a boy '

Split--Splitting a string into a sequence is the inverse method of join

>>> ' 1+2+3+4+5 '. Split (' + ') [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '] #如果不提供分隔符, then use a space as a separator >>> "I am a Boy". sp Lit () [' I ', ' am ', ' a ', ' Boy ']  

Strip--Remove the spaces on both sides

Translate--can replace some parts of a string, but unlike replace, he only handles a single character

Python learning 2-using strings

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.