Why does my Python code below for a school assignment work, and what does it do

Question:

For your information: I am new to coding in python and the code below isn’t mine, I’m just trying to learn how it works.

My teacher’s question is the following:

“Write code to input any string of length greater than 1 and print out the 2nd half of the string as the first output. Then, form a new string switching the fsecond half with the first half. Finally, print out the new string and make sure both outputs are in sentence form.”

My teacher stated that the code below works when I showed him it, so now I want to learn from it, the code was from a different site but the user, I believe a student, didn’t explain at all how it worked. He just posted it and woop it worked, so after trying 3 hours and promising that I wouldn’t use it several times. I did the logical thing that I could and copied and Pasted the code.

Research: I did about 2 hours’ worth of research, and this is what I understood

Line 1 = an input, asking the user the input a number and store it as a data.
Line 2 = to find string 1 takes the information of data and floor it by two, and take the largest integer taking the first half of the string.
Line 3 = taking the same thing as above, except if the string’s number of characters is odd, then take the bigger half. This is done by using the if statement that asks if the characters are even, then take the appropriate half like above but if not, then take the bigger half of something because of the “+0.”.

My Questions:
Honestly, I would appreciate it if you could explain the entire code and state if what I said was right or wrong.

But…
If not, then can you explain to me the following:
Why does the else statement has three parentheses, and then what does that do.
Why in the else statement does the “+0” effects the second half and how many characters it will have (i,.e the second string has more than the first”.
Why the square brackets are needed, and why it extended to the two strings? Should it not end after the first and have another square bracket on the second string.
Why there are “:” there. In both strings before the square brackets. Because I discovered if I remove the colons, the code turns funky and doesn’t work.

And thats it

Thank your for any help you can provide !!!

FYI 2 : I know the question asks for a string greater than 2, and so i thought putting an if statement would be good to ensure the user writes a string greater than 1 but my teacher said that wasn’t needed as that was self explanotory

The code:

data = input("Enter your string: ")
string1 = data[:len(data)// 2]
string2 = data[len(data)// 2 if len(data)%2 == 0
                else(((len(data)//2))+0):]
print(string2)
print(string2 + string1)

Answers:

Line 2 gives the first half of the string. The operation you see there is called slicing and it’s explained well here. Line 3 is equivalent to

string2 = data[len(data)// 2:]

The else produces len(data)// 2 as well, so you don’t need the else at all. I’m not sure why it’s there.

Answered By: Kraigolas

Word by word to better understand what’s going on.

data is a variable that you are creating. you could name this anything you want.

input() is a function that is already built into python that takes an input from the user. For this example let’s use ‘Hello World’

"Enter your string: " This is a parameter of input(). It is optional and could be message you want, it just lets the user know what to type in. In your question you stated it was taking a number, its not. It’s important to know the difference between strings, ints and floats.

string1 is another variable we are creating. can be named anything we want

data[] our stored variable from line 1 with brackets indicates that we are going to index through the set of characters stored in our variable data. So the first character ‘H’ stored at the 0 index, ‘e’ at the 1st index, and so forth. This way we can access each individual character (including the space)

So inside the brackets we can specify which characters we want to interact with.

: The colon is explained really well here.

len()is another function included in the python language that counts the number of items inside the object that placed inside the parenthesis. since we put the string of characters data inside we are going to get the number of characters in ‘Hello World’ which is 10 (remember we count starting with 0)

:len(data) So all together this says, from the beginning of the characters to the 10th character

// is an operator for floor division. It sounded like you understood what this means, basically divide by the following number and round down.

2 the number after one, before three 🙂

string2 another variable

data[len(data)// 2:] is the same as the one before, now the colon is at the end so we are only including the characters in data between the division floor to the end

print() another built in python function that displays an output to the user.

Hope that helps.

Answered By: cryptotheo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.