Created
October 23, 2020 10:02
-
-
Save lablnet/ff39c2a76d020447f46d673d48e4833e to your computer and use it in GitHub Desktop.
Simple list of python functions to manipulate strings and understand how they can works!q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _to_lower(text: str) -> str: | |
upper = "" | |
for i in text: | |
if 65 <= ord(i) <= 90: | |
upper += chr(ord(i) + 32) | |
else: | |
upper += i | |
return upper | |
def _is_upper(text: str) -> bool: | |
for i in text: | |
if 65 <= ord(i) <= 90: | |
return True | |
return False | |
def _to_upper(text: str) -> str: | |
lower = "" | |
for i in text: | |
if 97 <= ord(i) <= 122: | |
lower += chr(ord(i) - 32) | |
else: | |
lower += i | |
return lower | |
def _is_lower(text: str) -> bool: | |
for i in text: | |
if 97 <= ord(i) <= 122: | |
return True | |
return False | |
def _split(text: str, sep: str) -> list: | |
values,temp = [], "" | |
for c in text: | |
if c == sep: | |
values.append(temp) | |
temp = "" | |
else: | |
temp += c | |
if temp: | |
values.append(temp) | |
return values | |
def _count(sentence: str, substr: str) -> int: | |
count = 0 | |
for word in _split(sentence, " "): | |
if word == substr: | |
count = count + 1 | |
return count | |
def _is_digit(text: str) -> bool: | |
for i in text: | |
if 48 <= ord(i) <= 571: | |
return True | |
return False | |
text = "This is an example text, that we use in our example for testing the functions" | |
print("Text:", text) | |
print("Upper:", _to_upper(text)) | |
print("Is Upper:", _is_upper(text)) | |
print("Lower:", _to_lower(text)) | |
print("Is Lower:", _is_lower(text)) | |
print("Is Digit:", _is_digit(text)) | |
print("Split:", _split(text, ",")) | |
print("Count:", _count(text, "example")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment