Mike Driscoll Profile picture
Apr 20 β€’ 13 tweets β€’ 6 min read
#Python strings have TWELVE methods for testing string contents:

🐍 isalnum()
🐍 isalpha()
🐍 isascii()
🐍 isdecimal()
🐍 isdigit()
🐍 isidentifier()
🐍 islower()
🐍 isnumeric()
🐍 isprintable()
🐍 isspace()
🐍 istitle()
🐍 isupper()

πŸ§΅πŸ‘‡
Let's get started learning about these methods by looking at ` isalnum()`, which returns True if all characters in the string are alphanumeric and there is at least one character,
The `isalpha()` string method will return True if all characters in the string are alphabetic and there is at least one character
The `isascii()` string method will return True if the string is empty or all characters in the string are ASCII.

Here's an example that shows what happens with ASCII and Unicode (emoji)
The `isdecimal()` will return True if all characters in the string are decimal characters and there is at least one character,

Here's an example:
The `isdigit()` string method in #Python will return True if all characters in the string are digits and there is at least one character, False otherwise.

Digits include decimal characters and digits that need special handling
You can test a string to see if it can be an identifier using `isidentifier()`

Basically, you are testing if the string could be used as a variable or function name.

For full details on what an identifier is in #Python, see the docs:

docs.python.org/3/reference/le…
As you might expect, `islower()` tests to see if all the characters in a string are lowercase
The `isnumeric()` string method will return True if all characters in the string are numeric characters, and there is at least one character
The `isprintable()` string method in #Python is used to check if all the characters in a string are printable or not
The `isspace()` string method in #Python will return True if there are only whitespace characters in the string and there is at least one character

Here are a few examples:
The `istitle()` string method will "return True if the string is a titlecased string and there is at least one character, for example, uppercase characters may only follow uncased characters and lowercase characters only cased ones"
The `isupper()` string method in #Python will return Return True if all cased characters in the string are uppercase and there is at least one cased character

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Apr 21
Have you used #Python's `enum` module yet? No?

Then you should check out today's mini-thread!

🐍πŸ”₯πŸ§΅πŸ‘‡
Enum is short for enumerations which is a set of symbolic names (members) bound to unique, constant values.

Do NOT confuse the `enum` module with the `enumerate()` function though. 🐍⚠️

For full details on the `enum` module, see the docs:

docs.python.org/3/library/enum…
Let's look at an example of using the `enum` module.

In the following example, you subclass `enum.Enum`.

Then you add some enum values as class attributes, kind of like how you would with a dataclass

The `Enum` class supports iteration, so you iterate over it at the end Image
Read 7 tweets
Apr 20
Formatting strings can be done several different ways in #Python.

The most popular method now are f-strings.

However, today I am going to cover using the `format()` method in a mini-thread!

πŸπŸ§΅πŸ‘‡
The `format()` method can be used positionally. What that means is that you can have one or more empty curly braces and if you have enough arguments passed to `format()`, they will get inserted.

Here is an example:
If you pass in too few arguments to `format()`, then you'll get an exception 🐍⚠️
Read 9 tweets
Apr 13
Let's talk about profiling your #Python code today!

It's mini-thread time!

🐍πŸ”₯🧡
#Python comes with two code profiling modules built-in:

🐍 cProfile
🐍 profile

docs.python.org/3/library/prof…
Here is an example of profiling a small piece of code in #Python using cProfile: Image
Read 8 tweets
Apr 12
#Python generators aren't talked about enough.

So let's talk about them today!

πŸ§΅πŸπŸ‘‡
According to the #Python wiki, "Generator functions allow you to declare a function that behaves like an iterator"

One of their best use cases is to chunk through large data sets a piece at a time, which prevents you from running out of memory.

wiki.python.org/moin/Generators
To create a generator in #Python, you need to use the `yield` keyword instead of `return` inside a function.

Generators will continue to yield values until the generator is exhausted.

The following example will continue to yield numbers for as long as you call it:
Read 10 tweets
Apr 1
πŸ‘‹ Hi! I'm Mike

I tweet about:

🐍 Python programming
🐍 Python GUIs
🐍 Sys Admin with Python
🐍 Databases & Python
🐍 PDFs and Python
🐍 Excel and Python
🐍 XML with Python
🐍 Automation with Python
🐍 Beginner to advanced Python
🐍 Python tutorials & videos

🐍 🐍 🐍 🐍 🐍 🐍
You can check out my #Python 🐍 blog @mousevspython

blog.pythonlibrary.org
If you prefer to learn by watching videos rather than reading tutorials, I have a YouTube channel for your #Python learning too:

🐍 🐍 🐍 🐍 🐍 🐍

youtube.com/c/MouseVsPython
Read 5 tweets
Apr 1
Let's talk about test-driven development (TDD) with #Python today.

We will do that by going over a code kata example!

🧡🐍πŸ”₯πŸ‘‡
Test-Driven Development, or TDD, is a coding concept where you try to write a test BEFORE you write the new code.

Some TDD teachers recommend writing out the feature list and coming up with a small set of test ideas on paper first.

en.wikipedia.org/wiki/Test-driv…
The first step in TDD is to then write your first test, run it and verify that it fails.

You can write tests that pass that really shouldn't, after all. That is known as a useless test!
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(