" 2a 3ab"进入-2a -3ab。 正则表达式? :/ s =" 2a 3b" s =" - %s - %s"%tuple(s.split() ) -Larry In< 11 **************** ******@r56g2000hsd.googlegroups .com> ;, kevinliu23 写道: " 2a 3ab"进入-2a -3ab。 在[8]中:'' - ''+'' - ''。join(''2a 3ab 4xy''。split()) Out [8]:'' - 2a -3ab -4xy'' Ciao, Marc''BlackJack''Rintsch Hey guys, I want to be able to insert a ''-'' character in front of all numericvalues in a string. I want to insert the ''-'' character to use inconjunction with the getopt.getopt() function. Rigt now, I''m implementing a menu system where users will be able toselect a set of options like "2a 3ab" which corresponds to menuchoices. However, with getopt.getopt(), it''ll only return what I wantif I input -2a -3ab as my string. I don''t want the user have to inserta ''-'' character in front of all their choices, so I was thinking ofaccepting the string input first, then adding in the ''-'' charactermyself. So my qusetion is, how do I change: "2a 3ab" into "-2a -3ab". Regular expressions? :/ 解决方案 On Mar 30, 10:38 am, "kevinliu23" <[email protected]:Hey guys,I want to be able to insert a ''-'' character in front of all numericvalues in a string. I want to insert the ''-'' character to use inconjunction with the getopt.getopt() function.Rigt now, I''m implementing a menu system where users will be able toselect a set of options like "2a 3ab" which corresponds to menuchoices. However, with getopt.getopt(), it''ll only return what I wantif I input -2a -3ab as my string. I don''t want the user have to inserta ''-'' character in front of all their choices, so I was thinking ofaccepting the string input first, then adding in the ''-'' charactermyself.So my qusetion is, how do I change:"2a 3ab" into "-2a -3ab".Regular expressions? :/Regular expressions would definitely work. Here''s a hack though: tempInput = ''2a 3ab''tempLst = tempInput.split('' '') output = ''''for i in tempLst:output += (''-'' + i + '' '')I''m sure there are many better and more elegant hacks than this. Mike kevinliu23 wrote:Hey guys,I want to be able to insert a ''-'' character in front of all numericvalues in a string. I want to insert the ''-'' character to use inconjunction with the getopt.getopt() function.Rigt now, I''m implementing a menu system where users will be able toselect a set of options like "2a 3ab" which corresponds to menuchoices. However, with getopt.getopt(), it''ll only return what I wantif I input -2a -3ab as my string. I don''t want the user have to inserta ''-'' character in front of all their choices, so I was thinking ofaccepting the string input first, then adding in the ''-'' charactermyself.So my qusetion is, how do I change:"2a 3ab" into "-2a -3ab".Regular expressions? :/ s="2a 3b"s="-%s -%s"% tuple(s.split()) -LarryIn <11**********************@r56g2000hsd.googlegroups .com>, kevinliu23wrote: "2a 3ab" into "-2a -3ab".In [8]: ''-'' + '' -''.join(''2a 3ab 4xy''.split())Out[8]: ''-2a -3ab -4xy'' Ciao,Marc ''BlackJack'' Rintsch 这篇关于在字符串中的所有数字前面插入“ - ”字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 01:32