adam44a
Badges: 5
Rep:
?
#1
Report Thread starter 2 years ago
#1
Hi
How would I code the following on python
Change Hello World to HeLlO wOrLd
Thanks
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 2 years ago
#2
If any problem seems too difficult, take a step back and try to solve small parts of the problem on its own. The way to start is to write a program which just does something simple and maybe only solves one small piece of the problem before building on it.

For example:
- Do you know how to write a program in Python which can change a character to upper case?
- Do you know how to write a program in Python which can change a character to lower case?
- Do you know how to write a program in Python which can check whether a character is alphabetical?

Remember that a string is a collection of characters, so you can use Python for loops to cycle through each character in a string.

By the way, Python doesn't allow you to make modifications to strings. However that doesn't stop you, because Python does allow you to create new strings by appending (adding) characters using +=

For example, if you start with a blank string in Python, you can keep creating new strings by appending characters to the end: https://repl.it/repls/AllHugeVariable
Code:
my_string = ""
my_string += "c"
my_string += "a"
my_string += "t"
print(my_string)
So while you can't do an 'in-place' change to the original "Hello World" string, you can definitely pick out the characters from it one-by-one and then use that to help you solve the problem .
Last edited by winterscoming; 2 years ago
1
reply
adam44a
Badges: 5
Rep:
?
#3
Report Thread starter 2 years ago
#3
(Original post by winterscoming)
If any problem seems too difficult, take a step back and try to solve small parts of the problem on its own. The way to start is to write a program which just does something simple and maybe only solves one small piece of the problem before building on it.

For example:
- Do you know how to write a program in Python which can change a character to upper case?
- Do you know how to write a program in Python which can change a character to lower case?
- Do you know how to write a program in Python which can check whether a character is alphabetical?

Remember that a string is a collection of characters, so you can use Python for loops to cycle through each character in a string.

By the way, Python doesn't allow you to make modifications to strings. However that doesn't stop you, because Python does allow you to create new strings by appending (adding) characters using +=

For example, if you start with a blank string in Python, you can keep creating new strings by appending characters to the end: https://repl.it/repls/AllHugeVariable
Code:
my_string = ""
my_string += "c"
my_string += "a"
my_string += "t"
print(my_string)
So while you can't do an 'in-place' change to the original "Hello World" string, you can definitely pick out the characters from it one-by-one and then use that to help you solve the problem .
Okay...so how do I do it?
Just give me the code lol
0
reply
adam44a
Badges: 5
Rep:
?
#4
Report Thread starter 2 years ago
#4
(Original post by adam44a)
Okay...so how do I do it?
Just give me the code lol
It’s definitely possible
I was asked it on an online test
0
reply
winterscoming
Badges: 19
Rep:
?
#5
Report 2 years ago
#5
(Original post by adam44a)
It’s definitely possible
I was asked it on an online test
Did you make an attempt at it? Here's a possible way of doing it, but you could have many other variations too: https://repl.it/repls/ElderlyFluidGraphs
Code:
# Starting with an empty string to build up the resulting message
# because Python doesn't allow string modification 'in-place'.
message = ""

# Boolean flag which helps the loop alternate between upper and lower-case
# by alternating between True/False every time the loop encounters an
# alphabetical character.  First character is uppercase.
use_upper = True

# Deal with the "Hello World" string one character at-a-time in a loop.
# and repeat through to the end of the string.
for c in "Hello World":

    # Append a character as either upper or lower depending on the flag.
    if use_upper:
        message += c.upper()
    else:
        message += c.lower()

    # Only flip the upper/lower flag if the character is alphabetical
    # otherwise non-alpha characters will cause the flag to flip as well.
    if c.isalpha():
        use_upper = not use_upper

# Finished.
print(message)
The thing which makes it slightly more complex (the rationale behind adding a boolean flag) is the fact that you can't just choose upper/lower based on whether the position is an Even/Odd number because of that space character in the middle -- the space character means that you can't rely on character positions for the upper/lower logic, so that decision needs to be done differently. Booleans have two "states" (True/False) so they're handy for logic which has two different possible outcomes.


But programming is about the mindset behind solving problems like this algorithmically, and the best way to prepare for those online tests is to get practice. Even if you make the attempt and it doesn't quite work, that's so much more useful to you than not trying at all. The issue if you haven't tried it would be that you'd not really be any further forward than you were before next time you're taking an online test, so while I get that it's sometimes useful to look at solutions and see how they work, it's also a really valuable learning process to try and think around a problem that you might be stuck with.


By the way, there's a common pattern/theme which you'll see in loads of programming problems (since most programming problems usually end up dealing with some kind of list or collection)

1) The need to loop (iterate) through a collection or list of items (in this case, the collection is a string, which is like a collection-of-characters) -- that immediately lends itself to FOR X IN LIST:
2) Conditional logic check/s (IF/ELIF/ELSE) inside the loop which potentially does different things every time the loop repeats.
3) Having (one-or-more) variable/s that exist outside of the loop, which are modified within the loop based on that conditional logic.

Next time you run into a problem with a list or string or collection, try using 'for' and think about how variables from outside the loop can help track what's happening inside the loop.
Last edited by winterscoming; 2 years ago
1
reply
LouisOfar
Badges: 3
Rep:
?
#6
Report 2 years ago
#6
Print (“This is impossible”)
0
reply
Ir0nDwarf
Badges: 2
Rep:
?
#7
Report 2 years ago
#7
string = "Hello World"
newString = ""
case = False

for letter in string:

if letter == " ":

newString += " "
continue

if case == False:

newString += letter.upper()
case = True

else:

newString += letter.lower()
case = False

print(newString)


should work.
Last edited by Ir0nDwarf; 2 years ago
0
reply
LouisOfar
Badges: 3
Rep:
?
#8
Report 2 years ago
#8
You have to make an external note file on a seperate computer, hope you have an FTP account
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you tempted to change your firm university choice on A-level results day?

Yes, I'll try and go to a uni higher up the league tables (61)
29.05%
Yes, there is a uni that I prefer and I'll fit in better (15)
7.14%
No I am happy with my choice (118)
56.19%
I'm using Clearing when I have my exam results (16)
7.62%

Watched Threads

View All