Python基础入门之(数字、字符串)

>>> #字符串
>>> "meto",'meto'
('meto', 'meto')
>>> title = "Meaning" 'of' "lfe"
>>> title
'Meaningoflfe'
>>> 'knight\'s'
"knight's"
>>> "knight\"s"
'knight"s'
>>> s = 'a\nb\tc'
>>> s
'a\nb\tc'
>>> print(s)
a
b   c
>>> len(s)
5
>>> 
>>> s = '\001\002\x03'
>>> s
'\x01\x02\x03'
>>> print(s)

>>> #单引号、双引号、row字符串、三重那个引号
>>> mantra ="""Always look
on the bright
side of life"""
>>> mantra
'Always look\non the bright\nside of life'
>>> print(mantra)
Always look
on the bright
side of life
>>> s = 'hello'
>>> s[::-1]
'olleh'
>>> 
>>> 
>>> import sys
>>> print(sys.argv)
['']
>>>  int("42"),str(42)

SyntaxError: unexpected indent
>>> int('1')
1
>>> int("42")
42
>>> str(42)
'42'
>>> int("42") ,str(42)
(42, '42')
>>> ord('s')
115
>>> chr(115)
's'
>>> int('1101', 2)
13
>>> bin(13)
'0b1101'
>>> print(bin(13))
0b1101
>>> #字符串是不可变,所以不能修改
>>> 
>>> s = 'meto'
>>> L= list(s)
>>> L
['m', 'e', 't', 'o']
>>> L[2] = 'y'
>>> L
['m', 'e', 'y', 'o']
>>> s = ''.join(L)
>>> s
'meyo'
>>> #join函数,用什么去连接
>>> help(s.method)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    help(s.method)
AttributeError: 'str' object has no attribute 'method'
>>> 
>>> x=1234
>>> res = "integers: ...%d...%-6d...%06d" %(x,x,x)
>>> res
'integers: ...1234...1234  ...001234'
>>> #格式化表达式: 左对齐(-)、正负号(+),补零(0)
>>> 
>>> #基于字典字符串格式化
>>> reply = """
Greetings
Hello %(name)s!
your age squared is %(age)s

"""
>>> values ={'name':'meto', 'age':40}
>>> print(reply % values)

Greetings
Hello meto!
your age squared is 40


>>> vars()
{'L': ['m', 'e', 'y', 'o'], '__name__': '__main__', '__doc__': None, 'reply': '\nGreetings\nHello %(name)s!\nyour age squared is %(age)s\n\n', 's': 'meyo', 'values': {'name': 'meto', 'age': 40}, 'sys': <module 'sys' (built-in)>, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'title': 'Meaningoflfe', 'x': 1234, '__package__': None, 'res': 'integers: ...1234...1234  ...001234', '__builtins__': <module 'builtins' (built-in)>, '__spec__': None, 'mantra': 'Always look\non the bright\nside of life'}
>>> 
>>> 
>>> tem = '{1}, {2} and {3}'
>>> tem.format('spam', 'ham', 'eggs')
Traceback (most recent call last):
  File "<pyshell#66>", line 1, in <module>
    tem.format('spam', 'ham', 'eggs')
IndexError: tuple index out of range
>>> 
>>> 
>>> #不可变类型(数字、字符串、元组、不可变集合)
>>> #可变类型(列表、字典、可变集合)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值