1. 程式人生 > >RHEL/Centos7.x/python-2.7 Example

RHEL/Centos7.x/python-2.7 Example

 

[email protected]:~/Desktop$ python -V
Python 2.7.5
[email protected]:~/Desktop$ python 
Python 2.7.5 (default, Oct 11 2015, 17:47:16) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

 

1. String 
>>> print("hello")
hello
>>> print hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>> print "hello"
hello
>>> 

2. Int/String 

>>> int_a = 2 
>>> print int_a
2
>>> print int_a+"hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print str(int_a)+"hello"
2hello

 

3. Function 

######### translate int into a string with "%"

>>> def int_to_percent_string(int_para):
...         return str(int_para)+"%"           ####  ... is <tab> in text
... 
>>> print int_to_percent_string(22)
22%
>>> 
 

######### get a int number from a string like "xx%"

> def percent_to_int(string):
...     if "%" in string:
...         newint = int(string.strip("%")) 
...         return newint
...     else:
...         return int(0)
...         
... 
>>> print percent_to_int("22%")
22
>>> 

 

 

4. print utf8 string 

 

#-*- coding:UTF-8 -*-

string=xxxx   ### from uiautomator /device object attribute  .text
name=string.encode("utf-8")
print("Ascii puls utf-8 string "+name)