Python learning notes (7) Python string (4), learning notes python

Source: Internet
Author: User

Python learning notes (7) Python string (4), learning notes python

Input and Output

Input Function raw_input (Python3: input)

1 >>> raw_input ("enter a letter ") # To obtain A function 2 of the input content, enter A letter A 3 'A' 4 >>> 5 >>>> name = raw_input ("Please input your name :") # assign a value to the input content to the variable 6 Please input your name: tom 7 >>> name 8 'Tom '9 >>> x = raw_input ("how old are you? ") 10 how old are you? X = raw_input ("how old are you? ") 11 >>> x = raw_input (" how old are you? ") 12 how old are you? 2213 >>> x14 '22' 15 >>> type (x) # The result obtained through raw_input is a string type 16 <type 'str'> 17> help (raw_input) # view raw_input details 18 Help on built-in function raw_input in module _ builtin __: 19 20 raw_input (...) 21 raw_input ([prompt])-> string return value string 22 23 Read a string from standard input. the trailing newline is stripped.24 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z + Return), raise EOFError.25 On Unix, GNU readline is used if enabled. the prompt string, if given, 26 is printed without a trailing newline before reading.

 

Print () Format

Print is a statement in python2 and print () is a function in python3.

Format: specify a format and print it out in a certain format,

Create a template, leave some space in a certain part of the template, fill in some content on the space, then these spaces need some symbols to represent, usually called placeholders

% S string placeholder

% D integer placeholder

% F floating point placeholder

3 >>> "I like % s" 4 'I LIKE % s' 5 >>> "I Like % s" % "Python" # Fill % with Python after % s this placeholder 6 'I Like python' 7 >>> a = "I Like % s" 12 >>> a % "java" 13' I Like Java' 14 >>> print "% s was born in % d" % ("tom ", 2018) # The brackets are strings and numbers. 15 tom was born in 201816 >>>

Using braces {} index value placeholder Python {} is recommended {}

Str. format () format

1 >>> s = "I like {0}"2 >>> s.format("Python")3 'I like Python'4 >>> s2 ="%s was born in %d "%("tom",2018)5 >>> s2.format("tom",2018)6 'tom was born in 2018 '

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.