RHEL/Centos7.x/python-2.7 Example

版权声明:hushui版权所有 https://blog.csdn.net/hushui/article/details/83817935

lake@localhost:~/Desktop$ python -V
Python 2.7.5
lake@localhost:~/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%"

扫描二维码关注公众号,回复: 3999647 查看本文章

> 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)

猜你喜欢

转载自blog.csdn.net/hushui/article/details/83817935