
[ad_1]
You never know when you need to make your point
Well, then you must be wondering what is SpongeBob Mocking Converter. A meme made up of series with letters where every other character is lower/upper case would form a sentence like this:
WoW, yOu'Re ReAlLy SmArT!
Which technically means “Wow, you’re dumb.”
It takes some cognitive load to write like this, and it’s a bit much to be bothered with, to be honest. So, I decided it would be a good idea to create a Python tool that would do it for us.
Of course, such tools already exist online. If you want to try it, you can try This Service.
But let’s say you’re in the woods with your laptop for some reason, and you really need to take it offline.
It’s quite simple. You type your sentence and press enter. That’s it. Your sentence is returned in clear text for you to copy and paste into the chat application of your choice.
It will look like this:

It is clear that we will be working with the data type string
, We also need to find a way to convert the letters to upper case and lower case. Luckily for us, it’s already built into Python, and super simple to use.
string
String
We have many great ways to get things done. today we are looking upper()
And lower()
,
These two methods have the power to convert a letter to upper case or lowercase.
name="MaRtIn"
print(name.lower())>> martinprint(name.upper())>> MARTIN
They turn the whole string up or down. When we write code for our programs, we will operate on different characters, yet use the same method.
Let’s go through the code line by line – or rather than function by function. Here it is in full:
def_mock(sentence)
This function will use the parameter “sentence” and convert it to a new sentence where the letters are converted to a mixture of upper and lower case letters.
def mocker(sentence):
new_sentence = []
for index,letter in enumerate(sentence):
if index%2==0:
new_sentence.append(letter.upper())
else:
new_sentence.append(letter.lower())
return ''.join(new_sentence)
Here we can see how the function creates an empty list immediately (new_sentence
) The reason we do this is because we want to use a join at the end to reconstruct a sentence with our requirements.
To know which letter we need to set up or down, we are going to use the index of that letter. If you look at the words “Hello World” the index will look like this:
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
We want to say “if the index is divisible by 2, it will be upper.” This will give us all the even indices and their paired letters.
To be able to loop through the sentence and operate on the indexes, we would use enumerate()
, enumerate()
Allows us to access both the index and the value of the list item.
for index,letter in enumerate(sentence):
if index%2 == 0:
new_sentence.append(letter.upper())
When we use enumerate like this, we can use index to check where this letter is in sentence, and then we can add to our new list using letter as upper variation append()
,
We use modulo to determine whether the index number is odd or even. The reason this is a good technique is because you always have left over either 0
for even numbers or 1
for odd numbers. That way, you can decide what you want to do with the letters at the index that you return. 0
And what do you want to do with an index that returns 1
,
0%2 -> 0/2=0, R=0 (no remainder)
1%2 -> 1/2=0, R=1 (one remainder as you can devide 1 by 2, 0 times. Then you are left with 1.
...
7%2 -> 7/2=3, R=1
What we’ve told our script so far is that all letters with an even index will have upper case. The second situation does the opposite.
else:
new_sentence.append(letter.lower())
if you print new_sentence
list, you get this for “hello world”:
['H', 'e', 'L', 'l', 'O', ' ', 'W', 'o', 'R', 'l', 'D']
.Add()
So far, so good. However, a list is difficult to read. Because it is a list with a bunch of letters.
we want to use join()
To convert this list to a string which is more readable. The way a join works is that you can tell it to include all the items in a list and you can even customize which way you want to join it. for telling join()
What you want between your items, you add this inside quotes in front of the method:
'_'.join(new_sentence)
H_e_L_l_O_ _W_o_R_l_D
Obviously, we don’t want to add anything between our letters. We just want to add them. So we pass an empty string in front of the join method.
return ''.join(new_sentence)
>> Hello World
def main()
The only thing we need to do in main is to ask for a sentence and run it through a mocker function.
def main():
input_sentence = input('Type sentence to convert: ')
print(mocker(input_sentence))
Now that we have a functional script, we can start to consider minifying it. You see, this script can be a one-liner.
return(''.join([letter.lower() if index%2==0 else letter.upper() for index,letter in enumerate(sentence)]))
If you look at this sentence, you can see that it’s pretty much something we’re working on all the time.
- We are joining a range of characters to a list containing an empty string.
- We are creating this list using enumerate to access both the index and the value.
- We use the same terms as before.
if index%2==0
for lower case (or upper if you prefer), and using upper for odd indexelse
,
If you noticed while reading this story, you realized that there is a flaw in the code. It doesn’t account for spaces. If you have a sentence like this:
Hello World
it will read
HeLlO WoRlD
The “o” at the end of hello and the “w” of world are both capitalized because the space is read as a single character. It is then moved from uppercase space to lower case or vice versa. Check it out and see if you can find a way through the blanks.
It sounds weird, but I’ve actually used this tool several times over the past month. A silly tool, really, but a fun little exercise.
Thanks for stopping by.
Hope you enjoy writing the program.
[ad_2]
Source link
#Write #Sponge #Bob #Mocking #Converter #Python