一日平安一日福,一日快乐一日仙
环境
python3
Windows
打开终端进入 python 解释器,下面定义两个变量,并使用 type()
函数查看变量的类型。
>>> ip = '8.8.8.8'
>>> port = 80
>>> type(ip)
<class 'str'>
>>> type(port)
<class 'int'>
>>>
使用 help() 可以了解对应函数更多的信息
>>> help(str)
Help on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
使用 +
连接两个字符串,因为 port
是 int
类型,所以需要使用 str()
函数对其进行转换为 string
类型
>>> print("The IP is: " + ip + " and the port is: " + str(port))
The IP is: 8.8.8.8 and the port is: 80
>>>
对于字符串还可以进行 索引
,切片
,取长度
PS: 索引如果是负值将从右侧开始计数,负的索引从 -1 开始
>>> domain = 'zhutou.com'
>>> domain
'zhutou.com'
>>> domain[3]
't'
>>> domain[3:7]
'tou.'
>>> domain[8:]
'om'
>>> len(domain)
10
>>>
dir()
函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法 __dir__()
,该方法将被调用。如果参数不包含 __dir__()
,该方法将最大限度地收集参数信息。
>>> dir(ip)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
split
方法可以将字符串分割成列表,可以使用 help()
函数查看 split
方法的效果
>>> help(ip.split)
Help on built-in function split:
split(...) method of builtins.str instance
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
>>> string = ip + ':' + str(port)
>>> string
'8.8.8.8:80'
>>> string.split(':')
['8.8.8.8', '80']
>>>
同时可以使用索引获取列表中对应的值,使用 append
和 remove
方法对列表进行增删操作
>>> list = string.split(':')
>>> list
['8.8.8.8', '80']
>>> list.append('zhutou')
>>> list
['8.8.8.8', '80', 'zhutou']
>>> list.remove('zhutou')
>>> list
['8.8.8.8', '80']
>>> list[0]
'8.8.8.8'
>>>
文件的读写操作
>>> file = open('test.txt','w')
>>> file.write('Hello World')
11
>>> file.close()
>>> file = open('test.txt','r')
>>> file.readlines()
['Hello World']
>>>
python3 的基础巩固可以参考菜鸟教程。
猪头
2020.3.27