Python learning notes (7) Python string (4)

Posted by ThinkGeekness on Fri, 03 Apr 2020 03:48:34 +0200

Input and output

Input function raw Ou input (Python 3: input)

 1 >>> raw_input("Please enter a letter") #A function to get the input
 2 Please enter a letter A
 3 'A'
 4 >>>
 5 >>> name = raw_input("Please input your name:") #The input is assigned to a 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?22
13 >>> x
14 '22'
15 >>> type(x)  #The result is a string type through raw Ou input
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 specifies a format and prints it out in a format,

Make a template, leave some space in some place in the template, fill in some content on the space, then these spaces need some symbols to express, 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 the placeholder% s with Python after%
 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)  #String and number in parentheses
15 tom was born in 2018
16 >>>

Using curly braces {} to index value placeholder Python recommends {}

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 '

Topics: Python Unix Java Windows