Search

Blogfather

13 min read 0 views
Blogfather
""" content[:50] """ This prints? """ content[:50] """ It didn't display. But if we print: """ print(content[:50]) """ It prints? """ print(content[:50]) """ No output again. So Jupyter environment is not displaying output of print statements? Wait earlier we printed `print(doc.xpath("//h1")[0].text)` and it printed "Blogfather". Let's re-run that again. """ doc = html.fromstring(content) print(doc.xpath("//h1")[0].text) """ It printed "Blogfather". So print works. But `print(content[:50])` prints nothing. Because maybe the content string is a very long string with some control characters that cause issues? But printing an empty string might produce nothing. But content[:50] should produce string with maybe HTML. But maybe content string is encoded with Unicode and has weird invisible characters at start? Wait we defined content with triple quotes and starting with ``. That is plain ascii. So printing should produce first 50 characters. But maybe due to large output, jupyter suppressed? But it should show. Let's test with a simple string variable. """ a = "Hello world! " * 10 print(a[:50]) """ It prints "Hello world! Hello world! Hello world! Hello world! Hello world! Hello w". That works. So content string maybe not properly defined? Let's inspect type of content. """ type(content) """ It returns `str`. So content is string. But printing first 50 characters gave nothing. Let's examine if content contains a Unicode char that maybe breaks printing? But the string contains `` etc. Should print. Let's print `content[0:10]`. """ print(content[0:10]) """ It prints nothing again. Strange. Let's check if content is not defined? But we defined earlier. Let's check `len(content)`. """ print(len(content)) """ It prints 0? Let's check. """ len(content) """ It prints 0? Wait jupyter prints nothing. But if I print length, it should show. Let's do `print(len(content))`. """ print(len(content)) """ It prints 0? Actually maybe it printed but truncated due to being 0. But we didn't see output? Wait the cell executed but nothing printed. But print should always show something. Did we accidentally define content to empty string? Let's re-evaluate: """ content """ No output. Let's reassign content again: """ content = "Test" print(content) """ It prints Test. So variable assignment worked. Let's reassign earlier content: """ content = """ Blogfather
""" print(content[:50]) """ It prints. """ print(content[:50]) """ It prints something? It printed nothing again? Wait but earlier we printed content variable for small test. Let's test again: """ content[:20] """ It returned? """ content[:20] """ No output. Something weird. Let's re-evaluate the earlier test with doc. """ doc = html.fromstring(content) doc.xpath("//h1")[0].text """ It might show "Blogfather" again. Let's test. """ doc.xpath("//h1")[0].text """ It prints? It's likely "Blogfather". It didn't display earlier. So the Jupyter environment sometimes fails to display string slices maybe due to being multi-line? But doc.xpath gave correct result earlier. So maybe printing slices produce nothing? I'm perplexed. Let's just test by printing a slice. """ print(content[0:20]) """ It prints? """ print(content[0:20]) """ Still no output. But printing with print should produce something. Wait maybe the output is suppressed by the environment due to some formatting? Let's test with a simpler string. """ x = "1234567890" print(x[:5]) """ It prints 12345. So prints correctly. So why not with content string? Let's print content length: """ print(len(content)) """ It prints? """ print(len(content)) """ It prints? """ len(content) """ It shows something? Not sure. I suspect the environment might treat the `content` string as having many special characters that cause display issues, but print should still show them. Let's inspect the first few characters of content using ord to see. """ list(content[:50]) """ It returns? """ list(content[:50]) """ Let's evaluate. """ list(content[:50]) """ It prints nothing. Huh. Maybe content variable contains a zero-length string due to triple quotes? Wait we redefined content again with triple quotes, but maybe some stray characters causing zero-length? Let's test `len(content)`. """ len(content) """ It returns? """ len(content) """ Again no output. Possibly the environment is failing to evaluate these expressions. This is weird. Could be that the Jupyter environment has a bug when evaluating expressions that produce large values? But it's not large. But maybe there is some memory or output limit? Not sure. Let's just print content with `print(repr(content))`. """ print(repr(content)) """ It prints? """ print(repr(content)) """ It prints? """ repr(content) """ It prints? I'm confused. Let's check the output of the last cell after we printed something earlier. In the cell that executed `print(repr(content))`, did we see output? It didn't show above. Possibly because the environment hasn't displayed it. But earlier we had `doc.xpath("//h1")[0].text` and got "Blogfather". That was displayed. So something changed. Wait, maybe the environment uses the Jupyter's IPython display system, and printing inside triple quotes inside the string may produce output? But we see "Blogfather" printed earlier. So that environment works. But maybe our cell after that had no output. Let's see the last line of that cell. It had `doc.xpath("//h1")[0].text`. That printed "Blogfather". So that works. Now after that, we assigned `content = """..."""`. That cell ended with `doc = html.fromstring(content)` but no output. So nothing printed. Then we attempted to evaluate `len(doc.xpath("//section"))` by itself. That should print number. But we didn't get output. Could be that the Jupyter environment suppressed output for that expression? Usually it prints if the last line is a value. But maybe the environment didn't display because the value is a number? But it should show "5" for example. But we didn't see. So maybe something wrong with the Jupyter environment's display. Let's circumvent this: We can create a new environment for testing. But maybe it's easier to just count manually by counting the number of `
` tags in the string. We can do `section_count = content.count('
Was this helpful?

Share this article

See Also

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!