Thursday, October 19, 2017

Adventures in Python: String Manipulation and Function/Derivative Table (Subroutines)

Adventures in Python:  String Manipulation and Function/Derivative Table (Subroutines)

String Manipulation

This script demonstrates several string functions:

length:  the number of characters in a string

stringname.replace(‘target string’,‘new string’):  replace a part of a string

stringname.capitalize(): capitalize the first letter of a string

stringname.split():  splits a string at its spaces

Other string functions:

stringname.lower(): makes all letters of a string to lower case

stringname.upper():  makes all letters of a string to upper case

You can join strings with the plus symbol ( + ).

The nice thing is that you won’t need to import any modules for the above string functions.

Script:

# program 008
print('Program 008: String Demo')
str1 = input('Please type some text.  ')
# input defaults to strings
# length
print('Length')
l = len(str1)
print('Length',l)
# capitalizes the first letter
print('Capitalize')
str2 = str1.capitalize()
print(str2)
# string splits at spaces
print('Split')
str3 = str1.split()
print(str3)
# replacement
str4 = str1
str4 = str4.replace('a','e')
str4 = str4.replace('u','a')
str4 = str4.replace('o','u')
str4 = str4.replace('i','o')
str4 = str4.replace('e','i')
print('Can you read this?')
print(str4)

Output:  with the “This is my demonstration.”

Program 008: String Demo
Please type some text.  This is my demonstration.
Length
Length 25
Capitalize
This is my demonstration.
Split
['This', 'is', 'my', 'demonstration.']
Can you read this?
Thos os my dimunstritoun.


Function/Derivative Table (Subroutines)

You can create user functions (subroutines) in python by the def structure.  You return results by the return command.

Script (you can enter whatever your function you like, I have imported the math module):

# Program 009 Using def and creating a table
# import math
import math

def fx(x):
     f = math.exp(-x**2/2)
     return f;
    
print('Function Table')
beg = float(input('start: '))
end = float(input('stop: '))
stp = float(input('step: '))

# loop begins
h = .00001
k = beg
print('k',':','f(x)',';','slope')
while k <= end:
     # function
     a = fx(k)
     # derivative
     b = fx(k+h)
     c = fx(k-h)
     d = (b - c)/(2 * h)
     print(k,',',a,',',d)
     print('- - - - - - - -')
     k = k + stp
           

Output (with start = 0, stop = 5, step = 1)

Function Table
start: 0
stop: 5
step: 1
k : f(x) ; slope
0.0 , 1.0 , 0.0
- - - - - - - -
1.0 , 0.6065306597126334 , -0.6065306596914066
- - - - - - - -
2.0 , 0.1353352832366127 , -0.27067056647955834
- - - - - - - -
3.0 , 0.011108996538242306 , -0.033326989618259056
- - - - - - - -
4.0 , 0.00033546262790251185 , -0.0013418505118650097
- - - - - - - -
5.0 , 3.726653172078671e-06 , -1.8633265866508763e-05
- - - - - - - -

Eddie


This blog is property of Edward Shore, 2017.

No comments:

Post a Comment

Casio fx-CG 50 and HP Prime: The Price is Right Wheel Simulation

Casio fx-CG 50 and HP Prime: The Price is Right Wheel Simulation Introduction On the very famous game show, The Price Is Right, c...