Python Basics-strings, lists, tuples, dictionaries

Source: Internet
Author: User

String Introduction
    • Think about it:

      When the browser to login to some sites, you need to enter a password, the browser to send the password to the server, the server will verify the password, its verification process is to save the password and pass the past password to compare, if equal, then think the password is correct, otherwise it is considered wrong The server now wants to store these passwords in a database (such as MySQL), but for simplicity, we can find a variable to store the password, so how can I store a password with a letter?

    • For:

      String

The format of a string in <1>python

Variable A, as defined below, stores the value of a numeric type

A = 100

Variable b, as defined below, stores a value of type string

  " Hello itcast.cn "   or   'hello itcast.cn'

Small summary:

    • The data in double quotes or single quotes is the string
<2> string Output
Name ='xiaoming'position='Program Ape'Address='Beijing Basement'Print('--------------------------------------------------')Print("Name:%s"%name)Print("Position:%s"%position)Print("Company Address:%s"%address)Print('--------------------------------------------------')

Operation Result:

-------------------------------------------------- Name: Xiaoming Position: Program Ape Company Address: Beijing basement ----------------------- ---------------------------

String input

Note: The data obtained by input is saved as a string, even if a number is entered, and is saved as a string

Demo

UserName = input (' Please enter user name:')print(" Username:%s" %= input (' Please enter password:')print("  Password is:%s"%password)

Run Result: (different results depending on input)

Account name: Soaeon User name: Soaeon Please enter password: hhhhh password is: hhhhh

subscripts and slices

<1>. Subscript

The so-called “下标” , is the number, like the supermarket in the storage cabinet number, through this number can find the corresponding storage space

Use of "subscript" in a string

Lists and tuples support subscript indexes well understood, strings are actually arrays of characters, so subscript indexes are also supported.

If there is a string: name = ‘abcdef‘ , the actual storage in memory is as follows:

If you want to remove some of the characters, then you can go through 下标 the method, (note that the Python subscript starts from 0)

<2>. Slice

A slice is an action that intercepts a portion of an object that is manipulated. The string, list, and tuple support slicing operations.

Syntax for slices: [Start: End: Step]

Note: The selected interval is left closed right, starting from the start bit and ending with the last bit of the end bit (not including the end bit itself).

Let's take a string as an example to explain.

If you take a portion, you can use it in brackets [], using:

Common String Operations

If there is a string: mystr= ' How many fish is there in the pool ' the following are common operations

<1>find

Detects if STR is included in MyStr, or returns 1 if the index value is returned.

Mystr=' Howmany fish is therein the pool'result_one=mystr.find ('  how ') result_two=mystr.find ('how')Print  (str (result_one))print(str (RES))

<2>index

Just like the Find () method, only if STR does not report an exception in MyStr.

Mystr=' Howmany fish is therein the pool'result_one=mystr.index ('  how', 1,10)print(str (result_one))

<3>count

Returns the number of times that STR appears in the mystr between start and end

Mystr=' Howmany fish is therein the pool'result_one=mystr.count ('A  ', 1,10) result_two=mystr.count ('a')Print  (str (result_one))print(str (result_two))

<4>replace

Replace the str1 in mystr with the STR2 if count specifies that the replacement is not more than count times.

Mystr=' Howmany fish is there in the pool ha  ha'mystr.replace ('  Ha','ha') mystr.replace ('ha') ,'Ha', 1)

<5>split

Slice mystr with Str as delimiter, if Maxsplit has a specified value, only maxsplit substrings are delimited

Mystr=' Howmany fish is there in the pool ha  ha'mystr.split ('  ) mystr.split (")

Python Basics-strings, lists, tuples, dictionaries

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.