Journal: uffmm.org,
ISSN 2567-6458, July 15, 2019 – May 9, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de
Change: July 16, 2019 (Some re-arrangement of the content :-))
CONTEXT
This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.
SUBJECT
After a first clearing of the environment for python programming we have started with the structure of the python programming language, and in this section will deal with the object type string(s).
Remark: the following information about strings you can get directly from the python manuals, which you can find associated with the entry for python 3.7.3 if you press the Windows-Button, look to the list of Apps (= programs), and identify the entry for python 3.7.3. If you open the python entry by clicking you see the sub-entry python 3.7.3 Manuals. If you click on this sub-entry the python documentation will open. In this documentation you can find nearly everything you will need. For Beginners you even find a nice tutorial.
TOPIC: VALUES (OBJECTS) AS STRINGS
PROBLEM(s)
(1) When I see a single word (a string of symbols) I do not know which type this is in python. (2) If I have a statement with many words I would like to get from this a partition into all the single words for further processing.
VISION OF A SOLUTION
There is a simple software actor which can receive as input either single words or multiple words and which can respond by giving either the type of the received word or the list of the received multiple words.
ACTOR STORY (AS)
We assume a human user as executing actor (eA) and a piece of running software as an assisting actor (aA). For these both we assume the following sequence of states:
- The user will start the program by calling python and the name of the program.
- The program offers the user two options: single word or multiple words.
- The user has to select one of these options.
- After the selection the user can enter accordingly either one or multiple words.
- The program will respond either with the recognized type in python or with a list of words.
- Finally asks the program the user whether he/she will continue or stop.
- Depending from the answer of the user the program will continue or stop.
IMPLEMENTATION
Here you can download the sourcecode: stringDemo1
# File stringDemo1.py
# Author: G.Doeben-Henisch
# First date: July 15, 2019
##################
# Function definition sword()
def sword(w1):
w=str(w1)
if w.islower():
print(‘Is lower\n’)
elif w.isalpha() :
print(‘Is alpha\n’)
elif w.isdecimal():
print(‘Is decimal\n’)
elif w.isascii():
print(‘Is ascii\n’)
else : print(‘Is not lower, alpha, decimal, ascii\n’)
##########################
# Main Programm
###############
# Start main loop
loop=’Y’
while loop==’Y’:
###################
# Ask for Options
opt=input(‘Single word =1 or multiple words =2\n’)
if opt==’1′:
w1=input(‘Input a single word\n’)
sword(w1) # Call for new function defined above
elif opt==’2′:
w1=input(‘Input multiple words\n’)
w2=w1.split() # Call for built-in method of class str
print(w2)
loop=input(‘To stop enter N or Y otherwise\n’) # Check whether loop shall be repeated
DEMO
Here it is assumed that the code of the python program is stored in the folder ‘code’ in my home director.
I am starting the windows power shell (PS) by clicking on the icon. Then I enter the command ‘cd code’ to enter the folder code. Then I call the python interpreter together with the demo programm ‘stringDemo1.py’:
PS C:\Users\gerd_2\code> python stringDemo1.py
Single word =1 or multiple words =2
Then I select first option ‘Single word’ with entering 1:
1
Input a single word
Abrakadabra
Is alpha
To stop enter N
After entering 1 the program asks me to enter a single word.
I am entering the fantasy word ‘Abrakadabra’.
Then the program responds with the classification ‘Is alpha’, what is correct. If I want to stop I have to enter ‘N’ otherwise it contiues.
I want o try another word, therefore I am entering ‘Y’:
Y
Single word =1 or multiple words =2
I select again ‘1’ and the new menue appears:
1
Input a single word
29282726
Is decimal
To stop enter N
I entered a sequence of digits which has been classified as ‘decimal’.
I want to contiue with ‘Y’ and entering ‘2’:
Y
Single word =1 or multiple words =2
2
Input multiple words
Hans kommt meistens zu spät
[‘Hans’, ‘kommt’, ‘meistens’, ‘zu’, ‘spät’]
To stop enter N
I have entered a German sentence with 5 words. The response of the system is to identify every single word and generate a list of the individual words.
Thus, so far, the test works fine.
COMMENTS TO THE SOURCE CODE
Before the main program a new function ‘sword()’ has been defined:
def sword(w1):
The python keyword ‘def‘ indicates that here the definition of a function takes place, ‘sword‘ is the name of this new function, and ‘w1‘ is the input argument for this function. ‘w1’ as such is the name of a variable pointing to some memory place and the value of this variable at this place will depend from the context.
w=str(w1)
The input variable w1 is taken by the operator str and str translates the input value into a python object of type ‘string’. Thus the further operations with the object string can assume that it is a string and therefore one can apply alle the operations to the object which can be applied to strings.
if w.islower():
One of these string-specific operations is islower(). Attached to the string object ‘w’ by a dot-operator ‘.’ the operation ‘islower() will check, whether the string object ‘w’ contains lower case symbols. If yes then the following ‘print()’ operation will send this message to the output, otherwise the program continues with the next ‘elif‘ statement.
The ‘if‘ (and following the if the ‘elif‘) keyword states a condition (whether ‘w’ is of type ‘lower case symbols’). The statement closes with the ‘:’ sign. This statement can be ‘true’ or not. If it is true then the part after the ‘:’ sign will be executed (the ‘print()’ action), if false then the next condition ‘elif … :’ will be checked.
If no condition would be true then the ‘else: …’ statement would be executed.
The main program is organized as a loop which can iterate as long as the user does not stop it. This entails that the user can enter as many words or multi-words as he/ she wants.
loop=’Y’
while loop==’Y’:
In the first line the variable ‘loop’ receives as a value the string ‘Y’ (short for ‘yes’). In the next line starts the loop with the python key-word ‘while’ forming a condition statement ‘while … :’. This is similar to the condition statements above with ‘if …. :’ and ‘elif … :’.
The condition depends on the expression ‘loop == ‘Y” which means that as long as the variable loop is logically equal == to the value ‘Y’ the loop condition is ‘true’ and the part after the ‘:’ sign will be executed. Thus if one wants to break this loop one has to change the value of the variable ‘loop’ before the while-statement ‘while … :’ will be checked again. This check is done in the last line of the while-execution part with the input command:
loop=input(‘To stop enter N\n’)
Before the while-condition will be checked again there is this input() operator asking the user to enter a ‘N’ if he/ she wantds to stop. If the user enters a ‘N’ in the input line the result of his input will be stored in the variable called ‘loop’ and therefore the variable will have the value ‘==’N” which is different from ‘==’Y”. But what would happen if the user enters something different from ‘N’ and ‘Y’, because ‘Y’ is expected for repetition?
Because the user does not know that he/she has to enter ‘Y’ to continue the program will highly probably stop even if the user does not want to stop. To avoid this unwanted case one should change the code for the while-condition as follows:
while loop!=’N’:
This states that the loop will be true as long as the value of the loop variable is different != from the value ‘N’ which will explicitly asked from the user at the end of the loop.
The main part of the while-loop distinguishes two cases: single word or multiple words. This is realized by a new input() operation:
opt=input(‘Single word =1 or multiple words =2\n’)
The user can enter a ‘1’ or a ‘2’, which will be stored in the variable ‘opt’. Then a construction with an if or an elif will test which one of these both happens. Depending from the option 1 or 2 ther program asks the user again with an input() operation for the specific input (one word or multiple words).
sword(w1)
In the case of the one word input in the variable ‘w1’ w1 contains as value a string input which will be delivered as input argument to the new function ‘sword()’ (explanation see above). In case of input 2 the
w2=w1.split()
‘split()’ operation will be applied to the object ‘w1’ by the dot operator ‘.’. This operation will take every word separated by a ‘blank’ and generates a list ‘[ … ]’ with the individual words as elements.
A next possible continuation you can find HERE.