Day2-python Basic Knowledge: The method of the object

Source: Internet
Author: User
Tags string methods

The following methods are common string methods that are summed up by Jingliyang in the work.

1. Count () method

Role: The number of characters specified in the statistics string.

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.count (' o ')  # If there are multiple characters in the string being counted, Only the position of the first character is counted print (a)

The result returned is: 3

--------------------------------------------------------------------------------------------------------

2. Center () method

Function: Returns a string that is centered on the specified width, and returns the string if the specified number of widths is less than the length of the string.

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = str.center (+, ' * ') print (a)

return The result is:

www.oldboyedu.com***************

Process has ended, exit code

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.center (10, ' * ') # The length of the width is less than the length of the string, showing only the string itself. Print (a)

return The result is:

Www.oldboyedu.com process has ended, exit code 0

--------------------------------------------------------------------------------------------------------

3. Find () method

Function: Finds the specified character, returns the subscript position of the character, and returns a value of 1 if no character is found

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.find (' l ') print (a)

The result returned is: 5

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.find (' F ') print (a)

The returned result is: -1

--------------------------------------------------------------------------------------------------------

4. Index () method

Function: Similar to find, the difference is that the index () method will error if no value is found, and find () will not error.

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.index (' l ') print (a)

The result returned is: 5

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.index (' x ')   # Find characters not in a string print (a)

The returned result is:

Traceback (most recent):   " c:/users/william/pycharmprojects/s4/day2/Basic command. PY "  in <module>    = Str.index ('x'not  found process has ended, exit code 1

--------------------------------------------------------------------------------------------------------

5, Str.isalnum ()

Function: Detects whether a string consists of only letters and numbers

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.isalnum ()  # because the string in STR is containing punctuation, So it returns a value of Falseprint (a)

The returned result is:

False process ended, exit code 0

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' jingliyang666 ' a = Str.isalnum ()  # because the strings in Str consist only of strings and numbers, So the return is Trueprint (a)

The returned result is:

True the process has ended, exit code 0

--------------------------------------------------------------------------------------------------------

6, Str.isalpha ()

Function: Determines if the string is not a letter, if yes returns true, otherwise false

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboy.com ' a = Str.isalpha ()   # Because the string contains punctuation marks, So the return result is Falseprint (a)

The returned result is:

False process ended, exit code 0

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' Jingliyang ' a = Str.isalpha ()   # Because the string contains only letters, return Trueprint (a)

The returned result is:

True the process has ended, exit code 0

--------------------------------------------------------------------------------------------------------

7, Str.isdigit ()

Function: Determines whether a string is made up of numbers, or returns True if it is, otherwise false

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' www.oldboyedu.com ' a = Str.isdigit () # because the str string contains punctuation marks, it returns Falseprint ( A

The returned result is:

False process ended, exit code 0

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' 666666 ' a = Str.isdigit ()  # The string is made up of numbers, so return Trueprint (a)

The returned result is:

True the process has ended, exit code 0

--------------------------------------------------------------------------------------------------------

8, Str.islower ()

Function: Determines whether all letters in a string are made up of lowercase letters.

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' Jingliyang ' a = Str.islower () print (a)

The returned result is:

False process ended, exit code 0

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' Jingliyang ' a = Str.islower ()  print (a)

The returned result is:

True the process has ended, exit code 0

--------------------------------------------------------------------------------------------------------

9, Str.isupper ()

Function: Determines whether all letters in the string are uppercase, uppercase returns TRUE, otherwise false

Example 1:

#!/usr/bin/env python# Author:william huangstr = ' Jingliyang ' a = Str.isupper () print (a)

The returned result is:

True the process has ended, exit code 0

Example 2:

#!/usr/bin/env python# Author:william huangstr = ' Jingliyang ' a = Str.isupper () print (a)

The returned result is:

False process ended, exit code 0

--------------------------------------------------------------------------------------------------------

9, Str.maketrans ()

Unknown

Special thanks to the "old boy" Jing-yang teacher took the time to explain their valuable patience.

Day2-python Basic Knowledge: The method of the object

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.