top of page

String Methods

Python has a set of built-in methods that you can use on strings.

Note: All string methods returns new values. They do not change the original string.

For example, the upper() method returns a string where all characters are in upper case.

# example string string = "this should be uppercase!" print(string.upper()) # string with numbers # all alphabets whould be lowercase string = "Th!s Sh0uLd B3 uPp3rCas3!" print(string.upper())

Results:

>>> THIS SHOULD BE UPPERCASE! TH!S SH0ULD B3 UPP3RCAS3!

 
 

My code:

userWord = input("Enter a word: ") # Prompt the user to enter a word userWord = userWord.upper() # and assign it to the userWord variable. for letter in userWord: if letter == "A": continue elif letter == "E": continue elif letter == "I": continue elif letter == "O": continue elif letter == "U": continue print(letter.upper())

#############or just print(letter)

 
 

My code:

wordWithoutVovels = "" userWord = input("Enter a word: ") userWord = userWord.upper() for letter in userWord: if letter == "A": continue elif letter == "E": continue elif letter == "I": continue elif letter == "O": continue elif letter == "U": continue wordWithoutVovels += letter else: print(wordWithoutVovels)

 

Test data

Sample input: Gregory

Expected output: GRGRY

Sample input: abstemious

Expected output: BSTMS

Sample input: IOUEA

Expected output is empty

 

References:

aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page