EXAMPLE_TEXT = """TITLE: Why Computers are Stupid DESCRIPTION: The phrase "computers are stupid" refers to how they interpret instructions literally. This means that small typos can cause big problems. TITLE: Python DESCRIPTION: Python is a "programming language." It provides programmers a way to write instructions for a computer to execute in a way that the computer can understand. TITLE: Testing whether this really does work! DESCRIPTION: This is just some descriptive text to ensure that we have enough to be able to see the effect on the screen wondering whether characters such as these work as well: £$%&. TITLE: While Loops DESCRIPTION: A while loop repeatedly executes the body of the loop until the "test condition" is no longer true.""" def get_concept_by_number(text, concept_number): i = 1 start = 0 while i <= concept_number: i = i + 1 start = text.find("TITLE:",start) end = text.find("TITLE:", start+7) if end != -1: concept = text[start:end] start = end else: concept = text[start: ] return concept def get_title(concept): get = concept.find('TITLE:',0) end = concept.find('DESCRIPTION:',0) title = concept[get+7:end-1] return title def get_description(concept): get = concept.find('DESCRIPTION:',0) description = concept[get+13: ] return description def generate_concept_HTML(concept_title, concept_description): html_text_1 = ''' <div class="concept"> <div class="concept-title"> ''' + concept_title html_text_2 = ''' </div> <div class="concept-description"> ''' + concept_description html_text_3 = ''' </div> </div>''' full_html_text = html_text_1 + html_text_2 + html_text_3 return full_html_text def count_title(text): seek = 0 i = 0 seek = text.find('TITLE:',seek) while seek != -1: seek = seek + 7 seek = text.find('TITLE:',seek) i = i + 1 return i def html_complete(text): i=0 complete = '' concept_number = count_title(text) while i < concept_number: i = i + 1 concept = get_concept_by_number(text,i) concept_title = get_title(concept) concept_description = get_description(concept) complete = complete + generate_concept_HTML(concept_title, concept_description) return complete print html_complete(EXAMPLE_TEXT)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question