Strings Full
Strings Full
By
Naveen Kumar .V
String Literals
Strings can begin and end with double quotes, just as they do
with single quotes.
One benefit of using double quotes is that the string can have a
single quote character in it.
Escape Characters
if you need to use both single quotes and double quotes in the
string, you’ll need to use escape characters.
The escape characters \' and \" let you put single quotes and
double quotes inside your strings, respectively.
Raw Strings
Will consider the backslash as part of the string and not as the
start of an escape character.
Raw strings are helpful if you are typing string values that
contain many backslashes, mostly used for regular expressions
Multiline Strings with triple Quotes
A multiline string in Python begins and ends with either three single
quotes or three double quotes.
Any quotes, tabs, or newlines in between the “triple quotes” are
considered part of the string.
Python’s indentation rules for blocks do not apply to lines inside a
multiline string.
Multiline Comments
Strings use indexes and slices the same way lists do. You can
think of the string 'Hello world!' as a list and each character in
the string as an item with a corresponding index.
The in and not in operators can be used with strings just like
with list values.
An expression with two strings joined using in or not in will
evaluate to a Boolean True or False.
These expressions test whether the first string (the exact string,
case sensitive) can be found within the second string.
Useful String methods
The strings 'great' and 'GREat' are not equal to each other.
Here it does not matter whether the user types Great, GREAT,
or grEAT, because the string is first converted to lowercase.
The isupper() and islower() methods will return a Boolean True
value if the string has at least one letter and all the letters are
uppercase or lowercase, respectively. Otherwise, the method
returns False.
Since the upper() and lower() string methods themselves return
strings, you can call string methods on those returned string
values as well.
Expressions that do this will look like a chain of method calls.
The isX String Methods
Along with islower() and isupper(), there are several string methods
that have names beginning with the word is.
These methods return a Boolean value that describes the nature of
the string.
Common isX string methods:
• isalpha() returns True if the string consists only of letters and is not blank
• isalnum() returns True if the string consists only of letters and numbers
and is not blank.
• isdecimal() returns True if the string consists only of numeric characters
and is not blank.
• isspace() returns True if the string consists only of spaces, tabs, and new-
lines and is not blank.
• istitle() returns True if the string consists only of words that begin with
an uppercase letter followed by only lowercase letters.
The isX string methods are helpful when you need to validate
user input.
Eg:- This program repeatedly asks users for their age and a
password until they provide valid input.
Calling isdecimal() : - values in variables are decimal or not,
isalnum() :- values stored in alphanumeric or not.
The join() method is useful when you have a list of strings that
need to be joined together into a single string value. Its
called on a string
gets passed a list of strings
returns a string.
The returned string is the concatenation of each string in the
passed-in list.
The string join() calls on is inserted between each string
of the list argument.
Eg:- when join(['cats', 'rats', 'bats']) is called on the ', ' string, the
returned string is 'cats, rats, bats’.
The split() method does the opposite: It’s called on a string value
and returns a list of strings.
By default, the string 'My name is Simon' is split wherever
whitespace characters such as the space, tab, or newline characters
are found.
These white space characters are not included in the strings in the
returned list.
The center() string method works like ljust() and rjust() but
centers the text rather than justifying it to the left or right.
These methods are useful when you need to print tabular
data that has the correct spacing.
Using rjust(), ljust(), and center() lets you ensure that strings
are neatly aligned, even if you aren’t sure how many characters
long your strings are.
Removing Whitespace with strip(), rstrip(), and lstrip()
The pyperclip module has copy() and paste() functions that can
send text to and receive text from your computer’s clipboard.
It’s a bad habit to use the same password for each of them
( the hackers will learn the password to all of your other accounts)
Then you can copy any account password to the clipboard and paste
it into the website’s Password field.
Step 1: Program Design and Data Structures
But say you have a really large list that you want to add bullet
points to.
just type those stars at the beginning one by one
could automate this task with a short Python script.
Step 1: Copy and Paste from the Clipboard
You could write code that searches for each \n newline character
in the string and then adds the star just after that.
The lines list now contains modified lines that start with stars.
But pyperclip.copy() :-expecting a single string value, not a list
of string values.
Now the program is complete, and you can try running it with text
copied to the clipboard.