(Self-developed artificial intelligence) python string, artificial intelligence python

Source: Internet
Author: User

(Self-developed artificial intelligence) python string, artificial intelligence python

A string is the most commonly used data type in python. We can use single quotation marks ('') or double quotation marks (" ") to create a string.

a='Hello'b="Hello"

All standard sequence operations such as (index, sharding, membership, length, minimum value and maximum value) are also applicable to strings.

Common string formatting symbols:

(% S formatted string)

Print ('hello, % s' % 'World') # Use % s as the placeholder for 'World' Hello, world # result print ('and % s Day' % 10) # using % d as the placeholder for 10 is still 10 days # result # % s can not only format the string, but also format the integer

(% D format integer)

Print ('and % d Day' % 10) # Use % d as a placeholder for 10 and there are 10 days # result

Common Methods for string:

Find (): used to check whether a string contains a substring 'str'. You can specify the start and end ranges.

A = 'hello, world' print (. find ('wo') 6 # returns the index print (. find ('kb')-1 # If no value is found,-1 is returned.
Print (a. find ('wo', 3) # provide the starting point 6 # result print (a. find ('wd ', 6)-1 # result
Print (a. find ('Wo ',) # provide the start and end 6 # result print (a. find ('wd',)-1 # result

Lower (): converts all uppercase characters in a string to lowercase letters.

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

Upper (): converts all lowercase characters in a string to uppercase letters.

A = 'hello' B = a. upper () print (B) HeLlo # result

Swapcase (): converts all lowercase characters in a string to uppercase and lowercase letters to lowercase letters.

A = 'hello' B = a. swapcase () print (B) HeLlo # result

Replace (): replace the old string in the string with the new string.

A = 'Hello world' B = a. replace ('hello', 'Hello') print (B) hello world # result

Strip (): removes the specified character from the beginning and end of a string.

A = '++ hello world ++' B =. strip ('+') print (B) hello world # Result B =. strip ('++ H') print (B) ello world # Result B =. strip ('+ + D') print (B) hello worl # result

 

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.