0

If you've got a string containing for example "how are you?", I would do stringname.replace("how", "How") to have the first word written in Capital H.
So far so good.
The problem now is, I'm writing this script which at some point accesses the Open Weather Map, and the words are driving me crazy.
Until now I just did a

weather2 = self.weather.replace("partly", "Partly")
weather3 = weather2.replace("cloudy", "Cloudy")
weather4 = weather3.replace("foggy", "Foggy")
weather5 = weather4.replace("sunny", "Sunny")
weather6 = weather5.replace("rain", "Rain") #and so on

But I can't have 20 .replace().

So I was thinking, and here comes my question: Could I create two lists, one containing the OWM originals, and the other list with the Words it shall be replaced with, and do something like

for name in names 
    do something
Shade Reogen
  • 136
  • 2
  • 13

3 Answers3

2

To capitalize first letter use mystring.title()

for name in names:
  name.title()

https://www.geeksforgeeks.org/title-in-python/

Edmhs
  • 3,645
  • 27
  • 39
1

If you want to capitalize the string you can use .capitalize()

self.weather= self.weather.capitalize()

or if you want to use a list/dictionary solution:

dictionary={'cloudy':'Cloudy','foggy':'Foggy','sunny':'Sunny','rain':'Rain'}
if self.weather in dictionary:
    self.weather=dictionary[self.weather]
1

use .capitalize() function. .title() function also behaves similar. but it will capitalize all the first letter in a string if your string contains more than one word.

for name in names: name.capitalize()

kn_pro
  • 67
  • 1
  • 1
  • 11