操纵字符串 [英] manipulate string

查看:75
本文介绍了操纵字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说字符串是不可变的,但是无论如何都不能用
操作字符串?

我有一个字符串看起来像这样: br />
a =''0123456789''

但我希望它看起来像这样:

a =''0 - 2 - 4 - 6 - 8 - ''

我希望每个数字之间都有空格,我希望淡出每个

的第二个数字。

我可以明确地说这很有趣阅读该字符串的部分,打印[3]

完美无缺,但我无法删除它,删除它或者什么都没有。


我我会很感激如何解决我的小问题。


非常感谢。

问候,汤姆

解决方案

像这样:

导入字符串


a =''0123456789''

l = list(a)

b = string.join(l,'' - '')

print b


@ -salutations

-

Michel Claveau
$ b $bmél: http://cerbermail.com/?6J1TthIa8B


Michel Claveau / Hamster写道:

喜欢这个:

import string

a =''0123456789''
l = list(a)
b = string.join(l,'' - '')
print b



显然,OP希望丢弃索引1,3,5,7等处的字符......所以,

假设Python版本为2.3 ,正确的解决方案似乎是:

a =''0123456789''
print'' - ''。join(a [:: 2])



0 - 2 - 4 - 6 - 8


HTH

-

- Eric Brunel< eric dot brunel at pragmadev dot com> -

PragmaDev:实时软件开发工具 - http://www.pragmadev .com


Oups!对不起,我看得太快......



I heard that strings are immutable, but isn''t there regardless a way to
manipulate a string?
I have a string that looks like this:
a = ''0123456789''
But I want it to look like this:
a = ''0 - 2 - 4 - 6 - 8 - ''
I want whitespace between every number and I want to fade out every
second number.
It is funny that I can explicitly read parts of that string, print a[3]
works perfectly, but I can''t remove it, delete it or what ever.

I would appreciate any hint on how to solve my little "problem".

Thanks a lot.
Regards, Tom

解决方案

Like this :
import string

a=''0123456789''
l=list(a)
b=string.join(l,'' - '')
print b

@-salutations
--
Michel Claveau
mél : http://cerbermail.com/?6J1TthIa8B


Michel Claveau/Hamster wrote:

Like this :
import string

a=''0123456789''
l=list(a)
b=string.join(l,'' - '')
print b



Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7, etc... So,
assuming Python version is 2.3, the correct solution seems to be:

a=''0123456789''
print '' - ''.join(a[::2])


0 - 2 - 4 - 6 - 8

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com


Oups ! Sorry, i had read too fast...



这篇关于操纵字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆