python数据实例str

d:Program Filescmder
> python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 b
it (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+2
7
>>> 5-2
3
>>> 5*2
10
>>> 5/2
2.5
>>> 5//2
2
>>> "hello world"*10
'hello worldhello worldhello worldhello worldhello worldhello worldhello
worldhello worldhello worldhello world'
>>> "hello world" *10
'hello worldhello worldhello worldhello worldhello worldhello worldhello
worldhello worldhello worldhello world'
>>> "hello world "*10
'hello world hello world hello world hello world hello world hello world
hello world hello world hello world hello world '
>>> 5>2
True
>>> 5<2
False
>>> 5<>2
File "<stdin>", line 1
5<>2
^
SyntaxError: invalid syntax
>>> 5!=2
True
>>> 5=2
File "<stdin>", line 1
SyntaxError: can't assign to literal
>>> 5==2
False
>>> print("while"[1:3])
hi
>>> print("w h i l e"[1:3])
h
>>> str="hello world222"
>>> str[0:7:2]
'hlow'
>>> str[2:]
'llo world222'
>>> str[::2]
'hlowrd2'
>>> str[::-1]
'222dlrow olleh'

>>> mystr"hello world, python and java"
File "<stdin>", line 1
mystr"hello world, python and java"
^
SyntaxError: invalid syntax
>>> mystr "hello world, python and java"
File "<stdin>", line 1
mystr "hello world, python and java"
^
SyntaxError: invalid syntax
>>> mystr="hello world, python and java"
>>> mystr
'hello world, python and java'
>>> mystr.count("o")
3
>>> mystr.find("java")
24
>>> mystr.find("javah")
-1
>>> mystr.find("javahello")
-1
>>> mystr.index("javahello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

>>> str="hello world"
>>> str.replace("o","p")
'hellp wprld'

>>> lyric="一起看海"
>>> lyric.center(50)
' 一起看海 '
>>> lyric.ljust(50)
'一起看海 '
>>> lyric.rjust(50)
' 一起看海'
>>> lyric.zfill(50)
'0000000000000000000000000000000000000000000000一起看海'
>>> lyric.lstrip()
'一起看海'
>>> lyric.rstrip()
'一起看海'
>>> python="{} is {}"
>>> python.format("hello","world")
'hello is world'

>>> name=["老王","老李","老赵","老张"]
>>> for a in name:
... print(a)
File "<stdin>", line 2
print(a)
^
IndentationError: expected an indented block
>>> for a in name:
...    print(a)
...
老王
老李
老赵
老张

>>> name.append("Test")
>>> name
['老王', '老李', '老赵', '老张', 'Test']
>>> name.insert(0,"君子")
>>> name
['君子', '老王', '老李', '老赵', '老张', 'Test']
>>> name.delete("老李")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'delete'

-----------------------------------------------------------------

字典是无序的,所以没有索引值,查找不到也不会报错。

xxx[新key]=value   #增加

del xxx[key]   #删除

xxx[已存在的key]=new value  #修改

xxx.get(key)  #获取值

-------------------------------------------------------------------------

>>> infor={"name":"zhangsan"}
>>> infor
{'name': 'zhangsan'}
>>> infor["age"]=16
>>> infor
{'name': 'zhangsan', 'age': 16}
>>> infor["QQ"]=10086
>>> infor
{'name': 'zhangsan', 'age': 16, 'QQ': 10086}
>>> del infor[QQ]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'QQ' is not defined
>>> del infor["QQ"]
>>> infor
{'name': 'zhangsan', 'age': 16}

>>> infor["weixin"]=10010
>>> infor
{'name': 'zhangsan', 'age': 16, 'weixin': 10010}
>>> infor["weixin"]=22222222
>>> infor["QQ"]=998888888
>>> infor
{'name': 'zhangsan', 'age': 16, 'weixin': 22222222, 'QQ': 998888888}
>>> infor.get["QQ"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable
>>> infor.get("QQ")
998888888
>>> infor.get("QQ22")

原文地址:https://www.cnblogs.com/misswjr/p/9372336.html