Moving on to Ep.3 Method to the Madness we touch on... wait for it... Methods !
Methods are accessed using a command that follows the variable name immediately after a period (dot) and terminates in parentheses, which may or may not contain further arguments. In welcome.join(seq)
, join()
is the method. Methods are like functions that are specific to an object, such as a data type in this instance. The string type has the method join()
which takes a sequence as an argument and returns a single string made up of the elements of the sequence joined together with copies of the original string between them.
Q: Wow that sounds like a lot to do, what are the most popular methods() ?
A: So lets start with The upper(), lower()
String Methods. The upper() and lower()
string methods return a new string where all the letters in the original string have been converted to uppercase or lower-case, respectively. Nonletter characters in the string remain unchanged. So lets open up idle3 and give this code snip a try:
>>> welcome = 'hello world'
>>> welcome = welcome.lower()
>>> welcome
'hello world'
>>> welcome = welcome.upper()
>>> welcome
'HELLO WORLD'
>>>
Note that these methods do not change the string itself but return new string values.
If you want to change the original string, you have to call upper() or lower()
on the string and then assign the new string to the variable where the original was stored.
This is why you must use welcome = welcome.upper() to change the string in welcome
instead of simply welcome.upper().
print('Welcome to The Noobs of Python !\n Are you interested in learning python today?')
print('Yes | No | Maybe ?')
feeling = input()
if feeling.lower() == 'yes':
print('Great !')
elif feeling.lower() == 'no':
print('Well maybe next time !')
else:
print('You should consider participating, by either commenting or posting you code!')
The upper() and lower() methods are helpful if you need to make a
case-insensitive comparison. The strings 'YES' and 'YEs' are not equal to
each other. But in the previous small program, it does not matter whether
the user types YES , YeS , or yES , because the string is first converted to
lowercase. Adding code to your program to handle variations or mistakes in user
input, such as inconsistent capitalization, will make your programs easier
to use and less likely to fail.
Another set of methods that could be helpful would be the .isupper() and .islower()
Community Challenge: The isupper() and islower() methods will return a Boolean True value if the string has at least one letter and all the letters are uppercase or lowercase, respectively. Otherwise, the method returns False. So post your examples/questions about .isupper() and .islower() in the comment section
Q: Wow, that was cool, are there any more methods I would use regularly with Lists?
A:Lets talk about : The join() and split() String Methods. The join() method is useful when you have a list of strings that need to be joined together into a single string value. The join() method is called on a string, gets passed a list of strings, and returns a string. The returned string is the concatenation of each string in the passed-in list. Fire up idle3 again and lets try another code snippet! :
>>> sith =['Vader', 'Palpatine', 'Maul', 'Dooku']
>>> ' '.join(sith)
'Vader Palpatine Maul Dooku'
Q: So what does.split()
do?
A: The .split()
method does the opposite of .join()
It’s called on a string
value and returns a list of strings, so lets get back to idle3 :
`>>> 'Welcome to The Noobs of Python Ep.1'.split()
['Welcome', 'to', 'The', 'Noobs', 'of', 'Python', 'Ep.1']
>>>`
By default, the string 'Welcome to The Noobs of Python Ep.1' is split wherever whitespace characters such as the space, tab, or newline characters are found. These white
space characters are not included in the strings in the returned list. You can
pass a delimiter string to the split()
method to specify a different string to
split upon.
`>>> 'Welcome to The Noobs of Python Ep.1'.split('o')
['Welc', 'me t', ' The N', '', 'bs ', 'f Pyth', 'n Ep.1']
>>>`
A common use of split()
is to split a multiline string along the newline
characters so let's go back to idle3 once again:
`>>> example = '''What will we cover?
Simple code and some Regex to test the boundary of what we know.
I learn more when challenged and hopefully some of you do as well.
Post your questions or code snippets and lets learn together as a community !
Thanks to all involved
Miguel'''
>>> example.split('\n')
['What will we cover?', 'Simple code and some Regex to test the boundary of what we know.', 'I learn more when challenged and hopefully some of you do as well.', 'Post your questions or code snippets and lets learn together as a community !', '', 'Thanks to all involved', 'Miguel']
>>> `
Q: Wow. All this is great, It's time for me to get some practice in tolet it sink in...
A: Then hit the comment section with some code and ask if you need help! Enjoy
Thanks to all the community member contributing and asking for more. We will be going in depth into some of the Ep.x as we get more question from the comment sections. Leading up to the Intermediate 'The Knights Of Python'
Keep on coding Code_Warriors
Coming soon : The Noobs of Python : Ep.4 - What's your Function / Define Functions