楼主: Enthuse
1368 1

Python strings [推广有奖]

  • 4关注
  • 39粉丝

已卖:995份资源

大师

8%

还不是VIP/贵宾

-

威望
0
论坛币
75391 个
通用积分
825.9999
学术水平
103 点
热心指数
114 点
信用等级
86 点
经验
299244 点
帖子
12952
精华
0
在线时间
5848 小时
注册时间
2007-4-7
最后登录
2024-1-22

楼主
Enthuse 发表于 2015-5-4 23:07:55 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Python strings come with a powerful set of processing tools. Python has no distinct type for individual characters; instead you just
use one-character strings.

Strings are derived from sequences, and are immutable. It is easy enough to digest with examples:

S = ''   # empty string with single quotes
S ="spam's"  # double quotes
S ='s\n\p\ta\x00m'  # escape
S=""" ...multipline .."""  # triple-quoted block strings
S=r'\tmp\spam' # Raw strings (no escapes"
S=b'sp\xc4m'  # byte strings
S=u'sp\u00c5m'  # Unicode
S1+S2 # concatenation
S*3  
S[i]
S[i:j]
len(S)
"a %s parrot" % kind   # string formatting expression
S.find('pa')
S.rstrip()  # remove whitespace
S.replace('pa','xx')
S.split(',')
S.isdigit()
S.lower()
S.endswith('spam')
'spam'.jjoin(strlist)  # delimiter join
for x in S: print(x)  # iteration
'spam' in S
[c*2 for c in S]
map(ord, S)
re.match('sp(.*)am', line)  # pattern matching: library module

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:strings python string rings tring individual examples derived instead double

沙发
Enthuse 发表于 2015-5-4 23:38:50
notes:

index starts with 0, negative offset is added to the length of the string to derive a positive offset.

slicing s[:3] fetches items at offset 0 up to but not including 3

X[1:10:2] fetches every other item in X from offsets 1-9 with stride 2; it will collect items at offsets 1,3,5,7 and 9

strings can be converted between numeric types: int(S), float(S), str(1.234), str(1), repr(42), bin(13)

ord('s') can convert character to ASCII value, chr('115) is the reverse operation

s[0]='x' $ raises an error!

you need the following to replace:
s=s.replace('old', 'new')
s=s{:3]+'xx'+S[5:] # more powerful

build new text with formmating expressions
'That is %d %s bird!" % (1, 'beautiful')
'That is {0} {1} bird!'.format(1, 'beautiful')

Dictionary based formmating:
'%(qty)d more %(food)s' %{'qty':1, 'food':'spam'}
It is powerful when combined with vars() function

you can convert a string to a list, then making in place modifications
L=list(S)








您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 00:39