You're better off googling around for code that someone else created, read the API, and then move on.
As a side note, I've recognized an issue in a scripting style that I've used just recently, the problems very easily being solved by reading over the Python 3 documentation, which I've only done about once or twice, but not as heavily as I have recently. Built-in String methods. Everything in Python is an object of a class, in this case: str(). A few good ones are:
.startswith():
This tests for a specific set of characters at the beginning of a message. It's case sensitive, so be careful. It returns true.
.endswith():
Tests for the end of a statement, this can be useful, though is restricted as far as case is concerned. Returns True.
.upper():
Makes all of the letters inside of the string uppercase.
.lower():
Makes all letters in the text lowercase.
Pro Tip:
When I first started out, I used to make massive, and ugly logic:
yesorno = input("Y\n ")
if yesorno == "Y" or yesorno == "y":
somecommand()else:
someothercommand()
Now I just:
yn = input("[Y\n]: ")
if yn.lower() == "y":
somecommand()else:
someothercommand()
It's just neater, easier to read, and far less code.
.capitalize():
This capitalizes the first letter in the first word. I used it here to capitalize a name. Good for possibly forms that would require name data.
.casefold():
.
Similar to .lower() but instead removes all case specifications in general.
They can also be compounded.
I hope these thoughts were useful. To read more about string methods, visit the Python Documentation.
No comments:
Post a Comment