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 = 1234print("int is %d"%intTest)
%r原本替换
rtest = '"123'print("your input is %r"%rtest)
输出
your input is '"123'
使用terminal
from sys import argvprint('你输入的参数是:%r'%argv)from sys import argvprint('你输入的参数是:%r'%argv[1])
在terminal中输入
python StringTest.py yicunyiye
输出
0x02 列表你输入的参数是:['StringTest.py', 'yicunyiye']
你输入的参数是:'yicunyiye'
列表
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.d出
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'}
以上是内存溢出为你收集整理的python-字符串,字典,列表全部内容,希望文章能够帮你解决python-字符串,字典,列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)