-6

If I converted a string into lowercase, how would I convert it back to the original string the user entered? I have been searching for a particular python function that will convert the string back to the original string. I have used .swapcase() but unfortunately it didn't work. Please bear in mind that the .lower() is needed.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
k.joey
  • 17
  • 5
  • 8
    Why overwrite when you need the original one? Save it in a different variable. This looks like a case of [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Bhargav Rao Jun 23 '16 at 16:00
  • 2
    You can't, you have lost information by lower-casing it that you cannot get back. As @BhargavRao says, just **keep the original around too**. – jonrsharpe Jun 23 '16 at 16:01
  • How do you expect this function to work? How does it know whether `f("my string")` should return `"MY STRING"` or `"My String"` or `"My StRiNg"`, for instance? – Phylogenesis Jun 23 '16 at 16:01
  • "After I throw away some information, how do I get back the information I threw away?" - You can't; you have to keep the original information. – khelwood Jun 23 '16 at 16:02
  • Possible duplicate of [Case insensitive 'Contains(string)'](http://stackoverflow.com/questions/444798/case-insensitive-containsstring) – k.joey Oct 15 '16 at 14:19

3 Answers3

1

There is no function for this unfortunately. Rather than question why you need to do this, I would recommend keeping a separate list that keeps track of which characters in the string have been modified. Two approaches to this would keep:

1.) Keep a list of the indexes that in the original string were uppercase and were modified.

2.) Keep a list of length n, where n is the length of your string, where the value is true if the character was modified by .lower .

Using this style, you could write a helper function that does the lowercase method that creates this list for you as well as the complementary unlowercase method. Or, most logically, you could just save an instance of the original variable like everyone is saying.

matt.condit
  • 121
  • 1
  • 1
  • 15
0

You can't. "AbC" and "aBc" both turn into "abc" when lowercased. If an unlowercase method existed, how could it distinguish between those two cases? Should it return "AbC" or "aBc"? Keep in mind that strings have no "memory" of the letters they used to contain.

Kevin
  • 74,910
  • 12
  • 133
  • 166
0

You can't convert it back. What you CAN do is keep the original sting and just make a new variable for the lowercase one:

original = 'AbC'
lowered = original.lower()

What you could also do is store both values in a dict. For instance if the string is a name like 'John Smith' and you want to be able to use both 'John Smith' and 'john smith' later on you could do this:

name = {"original": "John Smith", "lowercase": "john smith"}

print(name["original"])
//prints 'John Smith'

print(name["lowercase"])
//prints 'john smith'

Here's a working example

I hope this helps with what you're trying to accomplish.

thatrandomguy
  • 56
  • 1
  • 6