Python learns-------variables and simple data types (String)

Source: Internet
Author: User

1. Variable naming and use

variable naming rules : A. Variable names can only contain (alphanumeric underscores), and variables cannot start with a number, for example: variable s_1 (correct), variable 1_s (error)

B. Variable names cannot contain spaces, you can use underscores to interval

C. Variables cannot be named with keywords and function names, for example: print

d. Variable names should be short and identifiable. (see the variable name to know what it might mean)

E. Avoid the use of easily confusing letters and numbers, such as 0 and 1,o and I (when the code is very large, this small error is not easy to find, try not to dig a hole for yourself)

2. Simple data types

String string: Use single quotation marks in Python or use double quotation marks to amplify strings.

For example: str = "Qazwsxedc" with str = ' Qazwsxedc ' means the same string, with the same effect.

Basic operation of String:

A. the case of the first letter of the string modified , using the method: string name. Title (), which is called the title method for example:

' Qazwsxedc '>>> str.title ()'qazwsxedc'

B. the case of all characters in the string , call the upper and lower methods, respectively. Upper () is to make all strings uppercase, lower the opposite. For example:

>>> str.upper ()'qazwsxedc'>>> str.lower () ' Qazwsxedc '

C concatenation of strings , using the plus sign for stitching, if required, a space string with two strings in the middle. For example:

 >>> str1 =  " adu  "  >>> str2 =  " you Has to more harder!!   " >>> str3 =str1 + str2  >> > STR3   "  Aduyou has to more harder!!   " >>> STR3 = str1 + "  "  + str2  >>> STR3   " adu you had to more harder!!   " >>> 

D. string interception , intercept part of the string: variable [head subscript: Tail subscript] (Note: The index value is starting from 0, 1 means starting from the tail, the output is the first (tail subscript), not (tail subscript) plus one. For example:

STR3 ='ADU Harder!!!'Print(STR3)Print(Str3[0:7])#starts from 0 to the end of the 7th character, including the space characterPrint(Str3[:-1])#-1 means starting from the tail and not writing the default from the beginning, that is, from the beginning to the second end of the last characterPrint(str3[2:])#output A third character laterPrint(STR3 * 2)#output String two times

#运行结果
ADU Harder!!!
Adu You
Adu harder!!
U has to work more harder!!!
ADU Harder!!! ADU Harder!!!
>>

Python learns-------variables and simple data types (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.