Python Data Types

Source: Internet
Author: User

Python Data Types

I. Data Types

1. Integer

In Python2, there are also integer and long integer types, but in Python3, the long integer type is removed and unified into integer type.

The integer numbers in Python can be expressed in binary, octal, decimal, or hexadecimal format.

The following method can be used to convert decimal to other forms.

>>> Print (bin (10) ---------> convert decimal to binary 0b1010 >>> print (oct (10 )) ---------> convert decimal to octal 0o12 >>>> print (hex (10) ---------> convert decimal to hexadecimal 0xa >>>> print (hex (100 )) 0x64

2. Floating Point Type

The floating-point number is a decimal point. It is called a floating-point number because the decimal point position can be changed when the decimal point is expressed by scientific notation. In computers, integers are always accurate, while floating-point numbers may produce errors during rounding. Small floating point numbers can be directly written, but for floating point numbers with many digits, they must be represented by scientific notation. If 10 is represented by e, for example, 1.03 multiplied by the 9 power of 10, it must be written as 1.03e9.

3. string type

It is defined as a string in quotation marks and is composed of characters.

Quotes can be single quotes, double quotes, three quotes, multiple quotes... (in Python, there is no difference between single quotes and double quotes)

Common Operations on strings:

Python provides many built-in methods for string processing.

1. strip () is used to remove white space. You can specify the characters to be removed in (). The default value is space.

X = ****** abc *****'
Print (x. strip ('*'))

Running result: abc

2. capitalize () uses uppercase letters

X = 'Hello world'
Print (x. capitalize ())

Running result: Hello world

3. The role of upper () is to write all letters in upper case. The opposite role of lower () is to write all letters in lower case.

X = 'Hello world'
Print (x. upper ())

Running result: HELLO WORLD

4. center (self, width, fillchar) is used to place the characters in the middle and fill the left and right with the specified characters to the specified length.

X = 'Hello world'
Print (x. center (30 ,'*'))

Running result: ********** hello world **********

5. count is used to count the number of times a character appears in a string. You can specify a range.
X = 'Hello world'
The print (x. count ('l', 1st) command counts the number of times 'l' appears among the to 11 characters in "hello world ".
Running result: 3

6. startswith () starts...
X = 'Hello world'
Print (x. startswith ('A') determines whether x starts with 'A'. If yes, the result is True. If not, the result is False.
. Endswith () ends...

7. find (). If the result is not negative, it is successful. Returns the position of the target character in the string.
The difference between find () and index () is that if the object cannot be found, find () will return-1, index () will report an error

8. format the string (here is a small example, which will be detailed in later blogs)
X = 'myname :{}, age :{}, sex :{}'
Print (x. format ('zhang ', 18, 'male '))
  
Running result: myname: zhang, age: 18, sex: male

9. view the index of a character in index (). If the index is out of the range, an error is returned.
X = 'Hello world'
Print (x [1]) ------> e
Print (x [4]) ------> o
Print (x [-1]) ------> d reverse count
Print (x. index ('W') ---------> 6. view the index of a character in the string.

String slicing: print (x []) --------> el: 1, 2 Characters in x
You can also add the step: print (x []) -------> el to take the 1, 2, and 3 Characters in x, but take each two characters, that is, take the 1, 3 Characters

10 .. isdigit () check whether the string is an integer. If yes, True is output; otherwise, False is output.
X = 'Hello world'
Print (x. isdigit ())
  
False

11. replace

X = 'Hello world'
Print (x. replace ('E', 'E') # not only can replace characters, but also can replace strings

Running result: hEllo world

12. split () is divided into several parts by space by default. Of course, you can also specify characters as separators.
  
X = 'Hello: world: today: is: a: goodday'
Print (x. split (':'))

The running result is: ['hello', 'World', 'today', 'is', 'A', 'goodday']

13. issupper () determines whether all are in uppercase
. Islower () determines whether all are in lower case
. Isspace () determines whether it is a space
. Istitle () determines whether the first letter is capitalized

14. ljust () left alignment
. Align ust () right alignment

15. swapcase () Case Inversion
Uppercase letters of. title ()



  
  


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.