|
| 1 | +# Strings are immutable. None of the changes affect the original variable unless assigned |
| 2 | +import string |
| 3 | + |
| 4 | +extractor="Hello World" |
| 5 | +print("Full String: ", extractor[:]) |
| 6 | + |
| 7 | +print("Offset from 3: ", extractor[3:]) |
| 8 | +print("Offset from :3 ", extractor[:3]) |
| 9 | +print("Offset from 3:8 ", extractor[3:8]) |
| 10 | + |
| 11 | +print("Last 3 characters: ", extractor[-3:]) |
| 12 | +print("Last 3 characters: ", extractor[4:-3]) |
| 13 | + |
| 14 | +print("By space: ", extractor.split(" ")) |
| 15 | + |
| 16 | +strip_variable = "This has punctuations ..!!" |
| 17 | +print("Strip Punctuations: ", strip_variable.strip(string.punctuation)) |
| 18 | +print("Strip White spaces: ", strip_variable.strip(string.whitespace)) |
| 19 | +print("Strip Punctuations and White spaces: ", strip_variable.strip(string.whitespace + string.punctuation)) |
| 20 | + |
| 21 | +print("Find the: ", strip_variable.find("the")) |
| 22 | +print("rFind the: ", strip_variable.rfind("the")) |
| 23 | +print("Find has: ", strip_variable.find("has")) |
| 24 | + |
| 25 | +# Returns substring not found error unlike -1 in find |
| 26 | +# print("Index the: ", strip_variable.index("the")) |
| 27 | +# print("rIndex the: ", strip_variable.rindex("the")) |
| 28 | + |
| 29 | + |
| 30 | +count_variable = "This has repeated texts for counting texts" |
| 31 | +print("Count: ", count_variable.count("text")) |
| 32 | + |
| 33 | +alpha_variable = "Thishasnumbersandtexts2" |
| 34 | +print("alpha_variable isAlphanumberic: ", alpha_variable.isalnum()) |
| 35 | +print("count_variable isAlphanumberic: ", count_variable.isalnum()) |
| 36 | + |
| 37 | +title = "This is a title" |
| 38 | +print("Title: ", title.title()) |
0 commit comments