Can someone please explain when we need to use an open string..I need more clarification please. Thanks | Sololearn: Learn to code for FREE!

+1

Can someone please explain when we need to use an open string..I need more clarification please. Thanks

8/18/2022 9:38:44 PM

Emmanuel Afolabi

15 Answers

New Answer

+2

Oh, this is a great question! I personally like using empty string when I need something temporary. For example, in the code below on Line 22, because when I want to store vowels that only occur at the very beginning of a word. My entire code is to get rid of vowels, but in the case of very first vowels such as "open", "output", "alarm", etc, I want to store them temporarily so that I can add them back later: https://code.sololearn.com/c1Rilc82eY89/?ref=app You may also find them useful when you need to make new words. By declaring an empty string first, you KNOW the variable will exist, you just haven't yet had the program add what you want to it yet. It's just up to your imagination! :D

+2

For #1, it means exactly what it is. Like say you're looking a store and you find something that you want to wear. You then ask your friend or partner to hold that item temporarily until you've done looking. It's the same thing. After you're done looking, you then ask for it back so that you can go pay for it. So in this example, your friend is the temporary variable or empty string. It'll give you that temporary variable back later to do what you want with. So in my previous example, I held onto certain letters and added them back later. That's all, nothing big or complicated. If I didn't have an empty string to hold the variable, I wouldn't be able to use it later in the same way that if your friend didn't hold the item you wanted, you couldn't pay for it (assuming you have too much in your own hands to carry LOL).

+2

I think generalizing what Justice said for nearly all data types could be a useful addition. You're initializing a variable, implicitly declaring(not the right word) its data type: x = "" y = [] z = {} S = set() This means: Initialize variable x as a string with None as the assigned default value, to be populated later on by values provided by my algorithm. y a list, z a dict, same logic applied What later follows is usually some conditionals and then actions taken to this end, assigning or changing values to these variables, also referred to as binding the variable to a value (stored at a certain location) What follows is: x += "something", because strings are immutable, id(x) or its location in memory is changed. y.append("someword") y is now a populated list ["someword" ], changed "in place" by a method, as list is a mutable object, same id. You'll see all this as you go along, what matters is this is not specific to types, it's just an empty bowl before you cook a recipe.

+2

Korkunç el Gato This is very true and totally slipped my mind! I often initialize empty data structures and string is a data structure as well (even though we still refer to it as a primitive data type for most languages).

+2

I should add that in Python, you can reassign a variable with an already "truthy" value of a certain data type (not None or False or 0: gives True if you put it in bool() ) with values that are of different types. x = "hello" x = 5 Python's garbage collector just removes all values left without a reference from memory. So x is now an integer. So why, then, do we care about what empty bowl we use? (string, list, etc) That’s when the methods and/or reassignments come into play. You initialize it, so you can change it using the method or functions etc only that data type allows. You can add to a set, but append to a list. So if you create an empty set first and try to append to it values, you'll get an error. You have to change it to a list first. Making it exist, as Justice said, to then use it. Conditionals isn't a must, it could be a loop too. It depends on what you want to do. These are just additions to what Justice said.

+2

try: x = "hello" print(x) print(id(x)) x = "world" print(x) print(id(x)) x = 5 print(x) print(id(x)) The id numbers point to the unique addresses in memory where the values are stored. x is a tag. Think of it like a price tag. You stick x on the first id location where information like "hello", its datatype etc are stored. (assignment) You then move x to another id location by reassigning it another value, "world". The moment the code to move that tag(reassignment line x = "world") is intetpreted, the old value "hello" is left without a reference, and garbage just clears up that memory address, making it available for storage again. now another reassignment, x = 5 Another id, and the same thing happens. The old value is dumped, memory location freed.

+2

As for methods: There are functions shared by more than one type. (becomes clearer in the classes section of python core) For example len() takes as argument all kinds of iterables. You can iterate over a string, list, dict et cetera. print(len("word")) print(len([1, 5, 7, 9])) You can use the method count on strings and lists: print("sssgg".count("s")) print([1, 1, 5, 7].count(1)) And then there are functions or methods specific to data type. If you're going to use one of these, initializing some variable with the empty value of another type is not going to make sense. If you aren't going to populate the empty slot with a value, then initializing a variable with an empty list, string etc doesn't make sense either, unless you have something very specific in mind that involves ysing the variable exactly like that, empty.

+2

Please have a look at this and https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables Following down the page, this: https://david.goodger.org/projects/pycon/2007/idiomatic/handout.html#python-has-names And then this: https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither// Lol, upon a message, I need to make clear: the beautiful tag analogy isn't mine, it's right there in the first two links that are actually successive sections on the same web page.

+1

Maybe you please clarify what you mean by "open string"? If you can provide a code bit, that would be even better so that we can see what you are referring to. Using the use of the word open includes file handling but I do not know if that is what you mean in this situation.

+1

This answers the question... Thanks a bunch.. 💯💯

+1

No problem at all!

+1

I'm sorry for questioning further If I understood you correctly, you made mention of reassigning a variable of different type and replacing it with another different one. Please, I really don't get this part 👇👇 "python's garbage collector just removes all values left without a reference from memory". And in the later part, the idea you are trying to flesh out is the fact that our methods has to tally with the data type we use. Is this correct sir?

0

Oh okay.... This is like assigning a variable to an empty string For example Output = ' ' , kind of situation. I don't know if you get me.

0

Wow I really appreciate the response. Although it is a lot to take in. All I could deduce is that 1. When you want to store data temporarily (please not clear enough as a newbie 😔) 2. Knowing that a variable will exists but the program that will be added isn't readily available. So in this case, an empty string is needed. (Clear enough)

0

These methods of these types (list, dic,set, strings) are usually followed truly when using an empty 'type'. And mostly they are used with conditionals thanks for shedding more light...