How do I convert a normal string in another format?

95 Views Asked by At

How do I convert a normal string in another format using Python script?

For exp: a normal "String" into BOLD "String" or other format fonts!

I tried with these two modules but without success:

import pyfiglet
str_string = "Hello World"
converted_text = ""
for char in str_string:
    if char.isalpha():
        converted_text += chr(ord(char) + 119743)
    else:
        converted_text += char
with open("converted_text.txt", "a+") as file:
    file.write(converted_text)

# OR

import unicodedata
def format_string(text):
    converted_text = ""
    for char in text:
        if unicodedata.category(char) == 'L':
            formatted_char = chr(ord(char) + 119743)
            converted_text += formatted_char
        else:
            converted_text += char
    return converted_text
str_string = "Hello World"
converted_text = format_string(str_string)
with open("converted_text.txt", "a+") as file:
    file.write(converted_text)

What I want to achieve is to convert from a normal string "Hello World" into format to look like this " " in the output.txt

I'm using QPython 3.6.6 in Android to convert my strings.

Thanks in advance

Respect

3

There are 3 best solutions below

1
On

To convert a string to lowercase, you can use the upper() method:

string = "hello world"
string_upper = string.upper()

To convert a string to uppercase, you can use the lower() method:

string = "HELLO WORLD"
string_lower = string.lower()

  
1
On

Text files do not have formatting notation. If you want to write this to display it in a HTML or other format, then please specify how will this ".txt" file be viewed.

0
On

These are nothing but unicode characters, so you can follow this way.

First, develop a function that will provide the Unicode code points of each individual character in the line.

def get_unicode_codepoints(sample):
"""
Returns a list of unicode codepoints for the given input string.
:param input_string:
:return:
"""
codepoints = [ord(char) for char in input_string]
return codepoints

Later, you can pass '' to the sample argument.

Now, create a dictionary in the format <english_letter>:<formatted_letter_codepoint>

def create_dictionary(codepoints):
    """
    Creates a dictionary with the English alphabet as keys and the given codepoints as values.
    :param codepoints:
    :return:
    """
    english_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    dictionary = {}

    for english_char, codepoint_char in zip(english_alphabet, codepoints):
        dictionary[english_char] = codepoint_char

    return dictionary

Finally, you can create a function that would perform the conversion.

def unicode_converter(formatted_sample, convertable):
    """
    Converts the given text using the Unicode codepoints from the formatted sample.
    :param formatted_sample: A string containing Unicode codepoints
    :param convertable: The text to convert.
    :return: Converted text.
    """
    codepoints_formatted = get_unicode_codepoints(formatted_sample)
    codepoints_dictionary = create_dictionary(codepoints_formatted)

    converted_text = ""
    for char in convertable:
        if char.isalpha() and char in codepoints_dictionary:
            converted_text += chr(codepoints_dictionary[char])
        else:
            converted_text += char

    return converted_text

CAUTION: DON'T FORGET ABOUT THE CHR() FUNCTION: chr(codepoints_dictionary[char]). Otherwise, codepoints_dictionary[char] returns integer, not string.

And make a startpoint for your app, where you apply all the things we've created:

if __name__ == "__main__":
    sample = ""
    convertable_text = input("Enter the text to convert: ")
    result = unicode_converter(sample, convertable_text)

    # Create a file named "output.txt" and write the result to it using UTF-8 encoding.
    with open("output.txt", "wb") as file:
        file.write(result.encode("utf-8"))

UPDATE: Since you would like to obtain a string from the requests.get(url) function, you can change __main__ function a bit, according to your needs. Other functions remain the same.

NOTE: The created file may not appear in your IDE Tree if it has not been included in your project. To confirm its presence, you can use your explorer to locate it.