The startswith and endswith functions in Python use instances, startswithendswith

Source: Internet
Author: User

The startswith and endswith functions in Python use instances, startswithendswith

In Python, there are two functions: startswith () and endswith (). The functions are very similar. The startswith () function determines whether the text starts with a certain character, endswith () the function determines whether the text ends with a certain character.

Startswith () function

This function determines whether a text starts with a certain number of characters and returns True or False.
Copy codeThe Code is as follows:
Text = 'Welcome to qttc blog'
Print text. startswith ('W') # True
Print text. startswith ('wel') # True
Print text. startswith ('C') # False
Print text. startswith ('') # True

Endswith () function

This function determines whether a text ends with one or more characters and returns True or False.
Copy codeThe Code is as follows:
Text = 'Welcome to qttc blog'
Print text. endswith ('G') # True
Print text. endswith ('Go') # False
Print text. endswith ('og ') # True
Print text. endswith ('') # True
Print text. endswith ('G') # False

Determine whether the file is an exe execution File

We can use endswith(registry.txt file name is not ended with .exe to determine whether the file is executable.
Copy codeThe Code is as follows:
# Coding = utf8
 
Filename1}'qttc.exe'
If(fileName1.endswith('.exe ')):
Print 'this is an exe execution file'
Else:
Print 'this is not an exe execution file'
 
# Execution result: This is an exe execution file.

Determine whether the filename suffix is an image

Copy codeThe Code is as follows:
# Coding = utf8
 
Filename1}'pic.jpg'
If fileName1.endswith('.gif ') or fileName1.endswith('.jpg') or fileName1.endswith('.png '):
Print 'this is an image'
Else:
Print 'this is not an image'

# Execution result: this is an image.


StartsWith () and endsWith () Methods of java string classes

First fill in ed
The second parameter is showMessageDialog (null, output)
This is the subject of the school examination?
This is boring only when the exam is answered.

Use of pow (x, y [, z]) functions in python

Your syntax is incorrect.
X and y in the built-in power (x, y [, z]) functions are mandatory, and z is an optional parameter. If the parameter z is used, brackets must be removed, that is, power (x, z, y, z). The result is that the y power of x is used to calculate the remainder of z. However, this method is more efficient than power (x, y) % z.

You can use power (2, 4) or power (2, 4, 3 ).
Power (2, 4) = power 4 of 2 = 16;
Power (2, 4, 3) = 4 power of 2 and then modulo 3 = 16% 3 = 1.

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.