Skip to content

Stringf guide

MathdallasAlt edited this page Mar 10, 2024 · 1 revision

How to use stringf?


Stringf is a python module that has a few functions for strings.

Functions-

  1. list_to_str()
  2. join()
  3. add()
  4. plural()

The list_to_str(list)function

This function converts any given list into a string. e.g.

x=["p","y","t","h","o","n"]
print(list_to_str(x)) 

Will print "python"

The code in the function is-

def list_to_str(list):
    strl=""
    for e in list:
        strl+= e
    return strl

Whereas list is the input(x in the previous example) given.

The join(word1,word2) function

This function joins two values. The values can be strings,integers and floats. e.g.

print(join("pyt","hon"))

Will print "python"

The code in this function is-

def join(word,word2):
    return str(word)+str(word2)

The add(word,pos,word2) function

This function is similar to the join() function but it doesn't join word2 after word1, it joins word2 after the letter at the specified position(pos) of word1. e.g.

print(add("pn",1,"ytho"))

The code in this function is-

word=list(word)
word.insert(pos,word2)
strl=""
for e in word:
    strl+= e
return strl

Will print "python"

The reverse(word) function

This function reverses a given string.

e.g.

print(reverse("python"))

Will return "nohtyp"

The code used in this function is-

word=list(word)
nword=word[::-1]
strl=""
for e in nword:
    strl+= e
return strl

Whereas word is the input(The string given in the previous example)given and nword reverses the list strl and the for e in nword loop is to convert the list into a string.

New!


The plural(word) function

This function is related more to English words than strings. This function converts any word to its plural form by a series of if statements.

e.g.

print(plural("python"))

Will return "pythons"

This functions is big because there are many if statements but if you really want to see it then here you go-

def plural(word):
vowel=list("aeiou")
consonant=list("bcdfghjklmnpqrstvwxyz")
word=list(word)
#Singular to plural conditionals
if word[-1]=="f" or word[-1]=="ef":
    word.append("s")
elif word[-1]=="s" or word[-1] == "x" or word[-1] == "z":
    word.append("es")
elif (word[-1]=="s" and word[-2]=="s") or (word[-1]=="s" and word[-2]=="h") or (word[-1]=="c" and word[-2]=="h"):
    word.append("es")
elif word[-1]=="s":
    word.append("ses")
elif word[-1]=="z":
    word.append("zes")
elif word[-1]=="y"and word[-2] in vowel:
    word.append("s")
elif word[-1]=="y" and word[-2] in consonant:
    word[-1]="ies"
elif word[-1]=="o":
    word.append("es")
elif word[-2]=="u"and word[-1]=="s":
    word.pop(-1)
    word[-2]="i"
elif word[-2]=="i"and word[-1]=="s":
    word[-2]="e"
elif word[-2]=="o"and word[-1]=="n":
    word.pop(-1)
    word[-2]="a"
elif word[-1]=="h":
    word.append("es")
else:
    word.append("s")
#List to string
strl=""
for e in word:
    strl+= e
return strl

More functions coming soon!