python01字串_python-字符串,字典,列表

0x01 字符串

python单双引号都可以

str = "hello world"

str_test = "yicunyiye"

print(str,str_test)

注释

#单行注释

"""

多行注释

"""

input你输入了任何东西都强转成字符串输出

str = "hello world"

str_test = "yicunyiye"

print(str,str_test)

print("hello \n world")

print(str_test+"\n"+str)

print("\t hello")

print("'")

print('"')

input_test = input('>>>')

print("你输入了:",input_test)

也可以c语言风格

intTest = 1234

print("int is %d"%intTest)

%r原本替换

rtest = '"123'

print("your input is %r"%rtest)

输出

your input is '"123'

使用terminal

from sys import argv

print('你输入的参数是:%r'%argv)

from sys import argv

print('你输入的参数是:%r'%argv[1])

在terminal中输入

python StringTest.py yicunyiye

输出

你输入的参数是:['StringTest.py', 'yicunyiye']

你输入的参数是:'yicunyiye'

0x02 列表

列表

split

序号切片

pop

append

len

remove

strTest = '1 2 3 4 5 6'

print(strTest.split(' '))

输出

['1', '2', '3', '4', '5', '6']

增删改查

1.添加

listTest.append("yicunyiye")

print(listTest)

输出

[1, 2, 3, 4, 5, 'yicunyiye']

2.弹出

print(listTest.pop())

输出

yicunyiye

原列表就没有yicunyiye了,相当于删除表尾元素

删除,写3就是删除3写'a'就是删除a

listTest = [1,2,'a',4,5]

listTest.remove('a')

print(listTest)

输出

[1, 2, 4, 5]

列表是从0开始的

print(listTest[0])

输出1

listTest = [1,2,4,5]

print(listTest[1:3])

输出[2, 4]

可以知道左闭右合

计算列表长度

print(len(listTest))

0x03 字典

增加

查找

删除

改变

取出所有

#键 值 对

dictTest = {"one":"yicunyiye","two":"wutang"}

print(dictTest)

输出

{'one': 'yicunyiye', 'two': 'wutang'}

增加

#增加

dictTest["three"] = "keji"

print(dictTest)

输出

{'one': 'yicunyiye', 'two': 'wutang', 'three': 'keji'}

删除

#删除

del dictTest["three"]

#dictTest.pop("two")

print(dictTest)

输出

{'one': 'yicunyiye', 'two': 'wutang'}

改变

#改变

dictTest["two"] = "yicunyiye"

print(dictTest)

输出

{'one': 'yicunyiye', 'two': 'yicunyiye'}

查找

#查找

print(dictTest["one"])

print(dictTest.get("two"))

输出

yicunyiye

取出所有

#取出所有

print(dictTest.items())

输出

dict_items([('one', 'yicunyiye'), ('two', 'yicunyiye')])

复制

#复制

newDict = dictTest.copy()

print(newDict)

输出

{'one': 'yicunyiye', 'two': 'yicunyiye'}

标签:dictTest,python,listTest,two,列表,yicunyiye,str,print,字典

来源: https://www.cnblogs.com/yicunyiye/p/13632547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值