forked from abhaysamantni/PythonReview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreAboutStrings.py
118 lines (80 loc) · 2.58 KB
/
MoreAboutStrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# This program concatenates strings.
def main():
name = 'Carmen'
print('The name is', name)
name = name + ' Brown'
print('Now the name is', name)
# Call the main function.
main()
# This program counts the number of times
# the letter T (uppercase or lowercase)
# appears in a string.
def main():
# Create a variable to use to hold the count.
# The variable must start with 0.
count = 0
# Get a string from the user.
my_string = input('Enter a sentence: ')
# Count the Ts.
for ch in my_string:
if ch == 'T' or ch == 't':
count += 1
# Print the result.
print('The letter T appears', count, 'times.')
# Call the main function.
main()
# This program gets the user's first name, last name, and
# student ID number. Using this data it generates a
# system login name.
import login
def main():
# Get the user's first name, last name, and ID number.
first = input('Enter your first name: ')
last = input('Enter your last name: ')
idnumber = input('Enter your student ID number: ')
# Get the login name.
print('Your system login name is:')
print(login.get_login_name(first, last, idnumber))
# Call the main function.
main()
def main():
# Create a string with a date.
date_string = '11/26/2012'
# Split the date.
date_list = date_string.split('/')
# Display each piece of the date.
print('Month:', date_list[0])
print('Day:', date_list[1])
print('Year:', date_list[2])
# Call the main function.
main()
# This program demonstrates the split method.
def main():
# Create a string with multiple words.
my_string = 'One two three four'
# Split the string.
word_list = my_string.split()
# Print the list of words.
print(word_list)
# Call the main function.
main()
# This program demonstrates several string testing methods.
def main():
# Get a string from the user.
user_string = input('Enter a string: ')
print('This is what I found about that string:')
# Test the string.
if user_string.isalnum():
print('The string is alphanumeric.')
if user_string.isdigit():
print('The string contains only digits.')
if user_string.isalpha():
print('The string contains only alphabetic characters.')
if user_string.isspace():
print('The string contains only whitespace characters.')
if user_string.islower():
print('The letters in the string are all lowercase.')
if user_string.isupper():
print('The letters in the string are all uppercase.')
# Call the main function.
main();