Python string concatenation + if while for loop example

Source: Internet
Author: User
Tags python string concatenation


Comments

Single-line Comment #

Multiline comment "' three single quotes or three double quotes" "

"Use three quotes to assign a multiple line

User interaction Input

string concatenation

+ ""% () "". Format () recommended

Name = input ("Name:")

age = Int (input (' Age: '))

Sex = input ("Sex:")

Example: +

# string concatenation +

INFO1 = '----info in ' + name + '---

Name: ' + name + '

Age: "+ Age +" "

Sex: ' + sex + '

'''

Example: ""% ()

#% format string

info = '------Info in%s-------

name:%s

age:%d

sex:%s

'% (' name ', ' name ', age, ' sex ')

# "". Format ()

Info3 = ' '---info in {_name}---

Name:{_name}

Age:{_age}

Sex:{_sex}

'. Format (_name=name,

_age=age,

_sex=sex)

Info4 = ' '---info in {0}---

NAME:{0}

Age:{1}

SEX:{2} '. Format (name, age, Sex)

Module definition:

Ciphertext password: Use after Getpass reference, Getpass.getpass ()

If Else uses

Cases:

Username = "username"

Password = "123456"

_username = Input ("Username:")

_PASSWD = Input ("Password:")

If username = = _username and Password = = _passwd:

Print ("Welcome user {Name} to Beij". Format (Name=username))

Else

Print ("Invalid username or passwd")

If elif Else

Cases:

Myage = 37

inputage = Int (input ("Please input Me Age:")

if inputage = = Myage:

Print ("It ' s Right")

Elif inputage > Myage:

Print ("small")

Else

Print ("Big")

While else loop

Count = 0

While Count < 3:

Myage = 37

inputage = Int (input ("Please input Me Age:")

if inputage = = Myage:

Print ("It ' s Right")

Break

Elif inputage > Myage:

Print ("small")

Else

Print ("Big")

Count+=1

Else

Print ("Fuck you!")

Break jumps out of the current loop

Continue out of the current loop and into the next loop

Homework

Write Login interface

Enter User name password
Show welcome information after successful authentication
Wrong three times, lock up.
Old_uname = open (R ' C:\Users\Administrator\Desktop\username.txt ', ' R '). ReadLines ()
Count = 0
While Count < 3:
Username = input ("Please your Username:")
passwd = input ("Please your passwd:")
For I in Old_uname:
If i = = Username:
Print ("Wolcome to your Blogs:{_uname}". Format (_uneme=username))
Break
Else
Continue
Else
Count + 1
If Count = 3:
Continue
Print ("The password you entered are incorrect!please input again ...")
Else
Print ("Three errors, account locked")
Open (R ' C:\Users\Administrator\Desktop\lockname.txt ', ' a '). Write (username + ' \ n ')

The


String base
(1), Escape string             
(2), Raw string--escape mechanism   open (R ' C:\tmp\a.txt ', ' A + ')
(3), Unicode string
(4), formatted string   "Age%d,sex%s,record%m.nf"% ( "Man", 73.45
String Base action
 + Connection, * Duplicate, S[i] Index (index), s[i:j] slice (slice), for loop traversal

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.