Nothing in life is as important as you think it is, while you are thinking about it.

Python – useful string methods

Hi all, here is a brief list of useful string methods. capitalize(). Makes the first letter of the string uppercase and all the rest lowercase, and returns the result. lower(). Returns a version of the string with all uppercase letters changed to lowercase. upper(). Returns a version of the string with all lowercase letters changed…
Read more

Python – Valid character list

Hi, let us check the given string against a valid character list. There might be numerous way to do this. Here is a sample one.   str_to_check = “1234-3*33?3+—-!” valid_chars = “0123456789-+” uyan_char_sayisi = 0 uymayan_listesi = “” uymayan_char_sayisi = 0 str_to_check_uzunluk = len(str_to_check) valid_chars_uzunluk = len(valid_chars) #print(str_to_check_uzunluk) for i in range(0,str_to_check_uzunluk): for j in…
Read more

Python – Average word count 2

Hi, here is a kind of follow up for the 1st one. We, in this exercise , deal with leading space(s) , Boolean and punctuation. def average_word_length(my_string): space_sayisi = 0 word_sayisi = 0 letter_sayisi= 0 ortalam_word_uzunlugu = 0 punctuation_sayisi = 0 punctuation_listesi = “.,!? ” try: uzunluk = len(my_string) except: return “Not a string” for…
Read more

Python – Error Handling 2

Hi all, here are more examples for error handling. my_value = 10 try: print(1 / my_value) print(“No error occurred!”) except NameError: print(“A NameError occurred!”) except ZeroDivisionError: print(“A ZeroDivisionError occurred!”) except TypeError: print(“A TypeError occurred!”) print(“Done!”) We catch three error types. try: print(undeclared_variable) #—> cause of a NameError print(1 / 0) #—> cause of a ZeroDivisionError…
Read more

Python – Error Handling 1

Hi all, here are the samples for error handling with try/catch. my_value = “0” try: print(10 / my_value) except ZeroDivisionError: print(“Can’t divide by zero”) except: print(“Not possible”) When you run the code, here is the output Not possible my_value = 0 try: print(10 / my_value) except ZeroDivisionError: print(“Can’t divide by zero”) except: print(“Not possible”) When…
Read more

Python – Average word count

Hi  all, this a function usage sample and it aims to find average word count in a given string.  We call word_count and letter_count functions in average_word_length function. That’s all 🙂 def word_count(a_string): space_count = 0 word_count = 0 for i in a_string: if i == ” “: space_count = space_count + 1 word_count = space_count…
Read more

Python – get current date

Hi, here is a sample for getting current date and print it. There are various string formating that you can choose regarding to your needs. In order to drop the leading zero (01/09/2018 > 1/9/2018) you can apply “-” or “#”. I added a comment for this. from datetime import date #gun1=gun.strftime(“%Y/%m/%d”) #print(gun.strftime(“%Y/%m/%d”)) #print(gun1)…
Read more

Python – Let’s count a word in a string

Hi, I guess this is not the best practice but it works 🙂 Let’s count “cat” in a given string. mystery_string = “why cat wy dog cat cacacat cacacat o cat bye cat cat” uzunluk_my_string = len(mystery_string) cat_sayisi = 0 #print(uzunluk_my_string) for i in range(0,uzunluk_my_string): #print(i) if mystery_string[i] == “c”: #print(i,” “,mystery_string[i]) if mystery_string[i+1] ==…
Read more

Python- multiplication / time table

Hi, here is a time table code for a given value. The tricky part is the 2nd print() function. It starts a new line. my_int = 5 for i in range(1,my_int+1): for j in range(1,my_int+1): print(i*j,end= “\t”) print() #The output is shown below 1 2 3 4 5 2 4 6 8 10 3 6…
Read more

Python – ord() and chr() functions

Hi, these functions, for example , convert a letter into its corresponding ascii code and vice versa. ord(A) –> give the value of 65 chr(65) –> would give “A” Here is a sample code for printing the alphabet 🙂 start_character = “A” end_character = “Z” for i in range(ord(start_character),ord(end_character)+1): print(chr(i))