Strings are Python builtins datatype for handling text. They are immutable thus you can not add, remove or updated any character in the string. If you wish to perform these operations than you need to create a new string and assign the existing/new variable name to it.
String is a sequence of characters
.
In [ ]:
an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence. An escape character is a particular case of metacharacters.
Escape sequence | Hex value in ASCII | Character represented |
---|---|---|
\a | 07 | Alert (Beep, Bell) (added in C89)[1] |
\b | 08 | Backspace |
\f | 0C | Formfeed |
\n | 0A | Newline (Line Feed); see notes below |
\r | 0D | Carriage Return |
\t | 09 | Horizontal Tab |
\v | 0B | Vertical Tab |
\ | 5C | Backslash |
\' | 27 | Single quotation mark |
\" | 22 | Double quotation mark |
\? | 3F | Question mark (used to avoid trigraphs) |
\nnnnote 1 | any | The byte whose numerical value is given by nnn interpreted as an octal number |
\xhh… | any | The byte whose numerical value is given by hh… interpreted as a hexadecimal number |
\enote 2 | 1B | escape character (some character sets) |
\Uhhhhhhhhnote 3 | none | Unicode code point where h is a hexadecimal digit |
\uhhhhnote 4 | none | Unicode code point below 10000 hexadecimal |
Strings can be classified in 2 categories.
In [ ]:
In [7]:
#### Standard String Examples:
friend = 'Chandu\tNalluri'
print(friend)
In [12]:
manager_details = "# Roshan Musheer:\nExcellent Manager and human being."
print(manager_details)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
+ Raw String: `a = r'Roshan\tMusheer'` # Roshan\tMusheer
+ Unicode String: `u = u'Björk'`
Since Python 3, strings are by default unicode string.
The standard string can be converted to unicode by using the function
unicode()
.
String can be initialized using:
s = r '\ n'
, where s
will contain the characters \
and n
).
In [7]:
a = r'Roshan\tMusheer'
print(a)
In [1]:
path = "C:\new_data\technical_jargons"
print(path)
path = R"C:\new_data\technical_jargons"
print(path)
NOTE: both r and R work the same way
In [9]:
a = 'Roshan\tMusheer'
print(a)
In [16]:
s = 'Camel'
print(id(s))
String concatenation is a process of joining two or more strings into a single string. As we have already discussed that string is an immutable datatype thus we have to create a new string for concatenation, what that means is the original strings will still remain the same and new one will be created using the texts from the originals.
There are multiple ways in which we can achive the concatenation. The most common method of achiving the concatenation, is to use +
operator.
Lets take an example, where we have three string's and lets try to concatenate them using it.
In [21]:
st_the = "The "
st_action = " ran away !!!"
st = st_the + s + st_action
print(st)
print(s)
print(st_the)
print(st_action)
print(id(st))
print(id(st_the))
print(id(s))
print(id(st_action))
In [3]:
print(dir(s))
string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
In [22]:
print( 'Size of %s => %d' % (s, len(s)))
print(dir(s))
print( 'Size of %s => %d' % (s, s.__len__()))
def size(strdata):
c = 0
for a in strdata:
c+=1
return c
print(size("Anshu"))
It is the new Interpolation method as it is implemented in Python 3.6
.
In [24]:
# name = 'World'
# program = 'Python'
# print(f'Hello {name}! This is {program}')
In [5]:
# String processed as a sequence
s = "Murthy "
for ch in s: print(ch , end=',') # This
# print(help(print))
print("\b.")
print("~"*79)
In [6]:
# Strings are objects
if s.startswith('M'): print(s.upper())
print(s.lower())
print("~"*79)
# what will happen?
print(3*s)
# print(dir(s))
In [7]:
s = " Murthy "
age = 5
print(s + str(age))
print(s.strip(), age)
# print(s + age)
In [17]:
st = " Mayank Johri "
print(len(st))
s = st.strip()
print(len(s))
print(st.rstrip())
print(st.lstrip())
In [13]:
m = "Mohan Shah"
x = ["mon", "tues", "wed"]
y = ","
a = "On Leave"
print(y.join(x)) # -> mon,tues,wed
print(m.join(y))
print(a.join(y))
print(y.join(a))
print(a.join(m))
Create a string from a list of string items
In [14]:
" ".join(x)
Out[14]:
In [15]:
book_desc = ["This", "book", "is good"]
" ".join(book_desc)
Out[15]:
The operator %
is used for string interpolation. The interpolation is more efficient in use of memory than the conventional concatenation.
Symbols used in the interpolation:
Symbols can be used to display numbers in various formats.
Example:
In [20]:
# Zeros left
print ('Now is %02d:%02d.' % (6, 30))
# Real (The number after the decimal point specifies how many decimal digits )
print ('Percent: %.1f%%, Exponencial:%.2e' % (5.333, 0.00314))
# Octal and hexadecimal
print ('Decimal: %d, Octal: %o, Hexadecimal: %x' % (10, 10, 10))
In [19]:
peoples = [('Mayank', 'friend', 'Manish'),
('Mayank', 'reportee', 'Roshan Musheer')]
# Parameters are identified by order
msg = '{0} is {1} of {2}'
for name, relationship, friend in peoples:
print(msg.format(name, relationship, friend))
In [36]:
# Parameters are identified by name
msg = '{greeting}, it is {hour:02d}:{minute:02d}'
print(msg.format(greeting='Good Morning', hour=9, minute=30))
print(msg)
# Builtin function format()
print ('Pi =', format(3.14159, '.3e'))
print ('Pi =', format(3.14159, '.1e'))
In [40]:
'{} {}'.format('सूर्य', 'नमस्कार')
Out[40]:
In [41]:
'{1} {0}'.format('सूर्य', 'नमस्कार')
Out[41]:
In [ ]:
In [ ]:
In [42]:
'{:>10}'.format('सूर्य नमस्कार')
Out[42]:
In [44]:
'{:20}'.format('सूर्य नमस्कार')
Out[44]:
In [49]:
'{:4}'.format('Bonjour')
Out[49]:
In [51]:
'{:_<5}'.format('Ja')
Out[51]:
In [58]:
'{:^7}'.format('こんにちは')
Out[58]:
In [ ]:
In [55]:
'{:.5}'.format('Bonjour')
Out[55]:
In [102]:
'{:10.5}'.format('Bonjour')
Out[102]:
In [106]:
'{:{align}{width}}'.format('Bonjour', align='^', width='9')
Out[106]:
In [ ]:
In [107]:
'{:.{prec}} = {:.{prec}f}'.format('Bonjour', 2.22, prec=4)
Out[107]:
In [ ]:
In [66]:
'{:d}'.format(1980)
Out[66]:
In [67]:
'{:f}'.format(3.141592653589793)
Out[67]:
In [72]:
'{:4f}'.format(3.141592653589793)
Out[72]:
In [77]:
'{:04d}'.format(119)
Out[77]:
In [68]:
'{:06.2f}'.format(3.141592653589793)
Out[68]:
In [78]:
'{:+d}'.format(119)
Out[78]:
In [79]:
'{:+d}'.format(-119)
Out[79]:
In [86]:
### Need to find for complex & boolean numbers
## '{:+d+d}'.format(-3 + 2j)
In [89]:
'{:=5d}'.format((- 111))
Out[89]:
In [ ]:
In [90]:
'{: d}'.format(101)
Out[90]:
In [ ]:
In [92]:
'{name} {surname}'.format(name='Mayank', surname='Johri')
Out[92]:
In [ ]:
In [95]:
user = dict(name='Mayank', surname='Johri')
'{u[name]} {u[surname]}'.format(u=user)
Out[95]:
In [ ]:
In [97]:
lst = list(range(10))
'{l[2]} {l[7]}'.format(l=lst)
Out[97]:
In [ ]:
In [100]:
from datetime import datetime
'{:%Y-%m-%d %H:%M}'.format(datetime(2017, 12, 23, 14, 15))
Out[100]:
In [ ]:
In [ ]:
In [ ]:
In [31]:
class Yoga(object):
def __repr__(self):
return 'सूर्य नमस्कार'
In [35]:
'{0!r} <-> {0!a}'.format(Yoga())
Out[35]:
In [ ]:
In [ ]:
In [3]:
myStr = "maya Deploy, version: 0.0.3 "
print(myStr.capitalize())
print(myStr.center(60))
print(myStr.center(60, "*"))
print(myStr.center(10, "*"))
print(myStr.count('a'))
print(myStr.count('e'))
print(myStr.endswith('all'))
print(myStr.endswith('.0.3'))
print(myStr.endswith('.0.3 '))
print(myStr.find("g"))
print(myStr.find("e"))
Note: The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator:
checking: substring in main_string : returns true or false
In [6]:
print("m" in myStr)
In [5]:
print("M" in myStr)
In [34]:
c = "one"
print(c.isalpha())
c = "1"
print(c.isalpha())
In [39]:
superscripts = "\u00B2"
five = "\u0A6B"
#str.isdecimal() (Only Decimal Numbers)
print(five)
print(c.isdecimal())
print(five.isdecimal())
print("10 ->", "10".isdecimal())
print("10.001".isdecimal())
str = u"this 2009";
print(str.isdecimal())
str = u"23443434";
print(str.isdecimal())
print(fractions.isdecimal())
In [42]:
# str.isdigit() (Decimals, Subscripts, Superscripts)
fractions = "\u00BC"
print(fractions)
print(c.isdigit())
print(fractions.isdigit())
print(five.isdigit())
print("10".isdigit())
str = u"this 2009";
print(str.isdigit())
str = u"23443.434";
print(str.isdigit())
In [29]:
print(superscripts)
print(superscripts.isdigit())
print(superscripts.isdecimal())
print(superscripts+superscripts)
print(fractions+fractions)
In [30]:
# str.isnumeric() (Digits, Fractions, Subscripts, Superscripts, Roman Numerals, Currency Numerators)
print(fractions)
print(fractions.isnumeric())
print(five.isnumeric())
In [ ]:
In [31]:
print(myStr.isalnum())
print("one".isalnum())
print("thirteen".isalnum())
In [14]:
import string
# the alphabet
print(dir(string))
a = string.ascii_letters
print(a)
# Shifting left the alphabet
b = a[1:] + a[0]
print(b)
print(b.__doc__)
print(string.digits)
print(string.hexdigits)
print(help(string.printable))
In [3]:
import string
# Creates a template string
st = string.Template('$warning occurred in $when $$what')
# Fills the model with a dictionary
s = st.substitute({'warning': 'Lack of electricity',
'when': 'April 3, 2002'})
# Shows:
# Lack of electricity occurred in April 3, 2002
print(s)
In [1]:
# Unicode String
u = u'Hüsker Dü'
# Convert to str
s = u.encode('latin1')
print (s, '=>', type(s))
# String str
s = 'Hüsker Dü'
# u = s.decode('latin1')
print (repr(u), '=>', type(u))
To use both methods, it is necessary to pass as an argument the compliant coding. The most used are "latin1" "utf8".