Let's see how we can apply the principles of sequence types to strings. We'll also see how to improve our output with the print function. Enter and run the following code:
first_name = 'John' last_name = 'Smith' full_name = first_name + ' ' + last_name print(full_name) print(len(full_name)) print(full_name[:len(first_name)]) print(full_name[-len(last_name):]) print(full_name.upper()) print('') n_pi = float(pi) n_e = float(e) print(n_pi) print("pi = " + str(n_pi)) print("pi = {0} e = {1}".format(n_pi, n_e)) print("pi = {0:.3f} e = {1:.4e}".format(n_pi, n_e))
The results should look like this:
We started out by defining two strings that represent a person's first name (given name) and last name (family name). We joined the strings using the +
operator, and computed the length of the combined string with the len
function. We then used some slice operations to extract the first and last name from the string containing the full name...