-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
  • 115,525
  • 42
  • 225
  • 281
Rakesh
  • 11
  • 2

2 Answers2

0

In Python, strings are immutable, change:

var.replace(',', '')

to

var = var.replace(',', '')
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
  • 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
  • 72,564
  • 26
  • 147
  • 178
BoppreH
  • 6,177
  • 3
  • 31
  • 63