-1

While trying basic python scripting in eclipse IDE, I am encountering problem: I just tried a simple code, which I found online:

var = 'hello , world'
print "%s" % var
var.strip(',')
print "%s" % var

The result i am getting is

hello , world
hello , world

Also i tried with replace command, but the result remain unchanged

var = 'hello , world'
print "%s" % var
var.replace(',', '')
print "%s" % var

The result obtained is

hello , world
hello , world

I could not figure out were I am making mistake.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Rakesh
  • 11
  • 2

2 Answers2

0

In Python, strings are immutable, change:

var.replace(',', '')

to

var = var.replace(',', '')
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Stack overflow should have a "someone else is also editing/answering this question" feature. We both made the same question edits and posted the same answer within seconds of each other. – BoppreH Aug 06 '15 at 01:58
0

Strings in Python are immutable. That means any methods that operate on them don't change the value, they return a new value.

What you need is

var = 'hello , world'
print "%s" % var
var = var.replace(',') # Replace the variable.
print "%s" % var

Also, print "%s" % var is redundant. You can just use print var.

Matt
  • 74,352
  • 26
  • 153
  • 180
BoppreH
  • 8,014
  • 4
  • 34
  • 71