(self-hing ai) Python string

Source: Internet
Author: User

Strings are the most commonly used data types in Python, and we can use single quotation marks (') or double quotation marks ("") to create strings.

A='hello'b="Hello"

All standard sequence operations such as (index, Shard, membership, length, minimum and maximum, etc.) are equally applicable to strings.

Formatting symbols commonly used in strings:

(%s formatted string)

Print ('hello,%s'%'World'# uses%s as a placeholder for ' world ' # Results Print (' also%s days '# uses%d as a placeholder for 10  # results #  %s can not only format strings, but also format integers

(%d formatted integer)

Print (' %d days '# uses%d as a placeholder for 10 # results

Common methods of strings:

Find (): Used to detect whether a string contains substrings of STR, you can specify a range of start and end.

 a= " hello, World   " print  (A.find ("  wo   "  6 #   Returns the index of the matching value  Span style= "COLOR: #0000ff" >print  (A.find ( " kb   " -1 #   not found, Returns-1   
 print  (A.find (  " wo  , 3)" #   Provide a starting point  6 #   result  print  (A.find ( '  WD   ", 6-1 #   result   
Print (A.find ('wo'# provides start and end # result print(A.find ( ' WD ', 3,7)# results

Lower (): Converts all uppercase characters in a string to lowercase

A='HeLlo'b=a.lower ()print# result

Upper (): Converts all lowercase characters in a string to uppercase

A='HeLlo'b=a.upper ()print#  Results

Swapcase (): Convert all lowercase characters in a string to uppercase, lowercase characters

A='HeLlo'b=a.swapcase ()print# Result 

Replace (): replaces the old string in a string with a new string

A='helloworld 'b=a.replace ('hello',' HELLO ' )print# results

Strip (): Removes the string from the Kinsoku specified character

A='++hello world++'b=a.strip ('+')Print(b) Hello world#Resultsb=a.strip ('++h')Print(b) Ello World#Resultsb=a.strip ('++d')Print(b) Hello Worl#Results

(self-hing ai) Python string

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.