-2

I'm trying to make a replacing some text to rest. Here my code is:

str = "2014년 8월 19일 오후 11:08, 회원님 : 안녕"
def convert(a):
    str.replace("년 ", a)
    str.replace("월 ", a)
    str.replace("일 ", a)

b = convert(",")
print(b)

and error like this:(pycharm IDE)

C:\Python34\python.exe C:/Users/minseok/PycharmProjects/untitled1/test.py
None

Process finished with exit code 0

I can't recognize my problem. What can i do for this error? thanks.

  • 1
    you should retrun the value form your convert function otherwise it return NOnetype – sundar nataraj Sep 24 '14 at 06:27
  • Partial duplicate of [Why doesn't calling a Python string method do anything unless you assign its output?](http://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – smci Mar 02 '15 at 10:01

3 Answers3

3

Two problems: first, your replacements aren't taking effect, and second, you're not returning anything. You need to do this:

def convert(a):
    myStr = "2014년 8월 19일 오후 11:08, 회원님 : 안녕"
    myStr = myStr.replace("년 ", a)
    myStr = myStr.replace("월 ", a)
    myStr = myStr.replace("일 ", a)
    return myStr

b = convert(",")
print(b)

replace does not modify the string, it returns a new string, so if you want to save the result, you need to assign it to a variable. Also, your function will return None unless you tell it to return something else, which is why you were seeing None.

Also, I moved your string inside the function. If you want to define it outside, you either need to pass it in as an argument, or use global myStr to make it a global variable (but the first option is better).

I also changed you variable name from str to myStr. str is the name of a builtin type in Python, so it's best not to use that name for your own variable.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • This gives an error: `UnboundLocalError: local variable 'myStr' referenced before assignment`. You either need to use a different name for the string inside the function, you you need to pass the string as an argument (so the first `replace` isn't being called on a global). – Blckknght Sep 24 '14 at 06:29
  • @Blckknght: You're right, I edited my answer to fix that as well. – BrenBarn Sep 24 '14 at 06:32
  • Sorry. but this code doesn't work properly. It is printing original value. – Minseok Jeon Sep 24 '14 at 06:39
  • @BrenBarn yes. as you replied, it works, but printing original value. – Minseok Jeon Sep 24 '14 at 06:45
  • @MinseokJeon: By "works", I mean it prints a value with some commas in it where the replaced strings used to be. – BrenBarn Sep 24 '14 at 06:46
1

replace returns a new string, so when you replace your chars, you miss the result. You should capture the tree results and then return it

>>> str = u"2014년 8월 19일 오후 11:08, 회원님 : 안녕"
>>> def convert(a):
...     return  str.replace(u"년 ", a).replace(u"월 ", a).replace(u"일 ", a)
...     
... 
>>> b = convert(", ")
>>> print(b)    
2014, 8, 19, 오후 11:08, 회원님 : 안녕
xecgr
  • 5,095
  • 3
  • 19
  • 28
0

You are making two mistakes. First, you are shadowing built-in name str and second, you are not returning anything from convert(a) that is why None is printed on the string.

Look at the following code snippet for help,

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

sample_string = "2014년 8월 19일 오후 11:08, 회원님 : 안녕"


def convert(a):
    string_to_return = ''

    string_to_return = sample_string.replace("년 ", a)
    string_to_return = string_to_return.replace("월 ", a)
    string_to_return = string_to_return.replace("일 ", a)

    return string_to_return

if __name__ == '__main__':

    b = convert(",")
    print(b)
Saleem Latif
  • 1,482
  • 12
  • 8