6.9k views
0 votes
You have a string variable named greeting with value "Hello, world". If you want to change the first letter to produce the output "Yello, world" you type:

A. greeting[0] = 'Y'
B. greeting.replace('H', 'Y')
C. greeting[1] = 'Y'
D. greeting.swapcase()

User Idan Aviv
by
8.0k points

1 Answer

0 votes

Final answer:

To change 'Hello, world' to 'Yello, world', use the method greeting.replace('H', 'Y') since strings are generally immutable and cannot be altered by index.

Step-by-step explanation:

To change the first letter of the string variable greeting with the value "Hello, world" to 'Y' to produce the output "Yello, world", you should use option B: greeting.replace('H', 'Y'). This is because strings in many programming languages are immutable, which means they cannot be changed after they are created. So, you cannot directly change a character at a specific index like in option A. Using the replace method creates a new string with the specified changes.

Options A and C are incorrect because you typically cannot assign a value to a string index. Option D, swapcase(), is incorrect because it would invert the case of all the letters in the string, not change 'H' to 'Y'.

User Tim Dunphy
by
7.4k points