1.1 python string definitions
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
-
# Definition of a string
-
s1 = 'this is long String that spans two lines'
-
-
# Indicates the following line is a continuation of the previous line
-
s2 = 'this is long String\
-
that spans two lines'
-
-
# Is output as a string
-
s3 = """this is long String
-
that spans two lines
-
"""
-
# Newline character string output
-
s4 = 'this is long String\n\
-
that spans two lines'
-
-
print "s1=",s1
-
print "s2=",s2
-
print "s3=",s3
-
print "s4=",s4
Output:
Conversion between the character and the character value 1.2 python
A: & characters into numeric value into a string: ord function which can be converted to a string is an integer ranging from [0,65535]
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
# Characters into value
-
s = 'a'
-
print ord ( s )
-
# Value into a string
-
b = 97
-
print chr(b)
B: Parameters args chr () function in the [0,256], meaning that only the [0,256] into a string of digital
C: The value is a Unicode code into a Unicode string
-
#!/usr/bin/python
-
# -*- coding: utf8 -*-
-
-
# The values were converted to unicode string code
-
print repr(unichr( 8224))
-
# Unicode string into the code values
-
print repr (words ( u '\ u2020' ))
D: chr () function and the difference between str function:
1. chr () function is as a parameter to a small integer, and returns the ASCII character string corresponds to a single character
2. str () function is to any integer as a parameter and returns the integer a text string