<jupyter_start><jupyter_text>String Operations<jupyter_code># Strings are immutable types, once created we can not modify a string. Any operation on string will result
# in a new string
country = "India"
# strings are stored as lists (non-mutable), we can access each character by its index
print(country[0])
print(country[1])
# We can iterate over each element in string by index
for character in country:
    print(character, end='-') # Using end as an argument print acts as a de-limiter and prints 
                              # the characters accordingly<jupyter_output><empty_output><jupyter_text>Basic Formatting<jupyter_code>user = input("Enter username: ")
'Hello user: {}'.format(user)
# Raw formatting
'Numbers are {} {}'.format(1,2)
# Formatting with index of arguments, an option to change placeholders ! 
'Numbers are {1} {0}'.format(6,7)<jupyter_output><empty_output><jupyter_text>Padding and Aligning Strings<jupyter_code>'{:>10}'.format('test')
'{:<10}'.format('test')
'{:10}'.format('test')
'{:_<10}'.format('test')
'{:_>10}'.format('test')
'{:^10}'.format('test')
'{:_^10}'.format('test')<jupyter_output><empty_output><jupyter_text>Truncating long strings<jupyter_code>'{:.5}'.format('xylophone')
'{:10.5}'.format('xylophone')
'{:_<10.5}'.format('xylophone')
'{:_>10.5}'.format('xylophone')<jupyter_output><empty_output><jupyter_text>Numbers<jupyter_code>'{:d}'.format(42) #Ints
'{:f}'.format(3.141592653589793)
# Padding numbers
'{:4d}'.format(42)
'{:04d}'.format(42)
# Padding and truncating float
'{:06.2f}'.format(3.141592653589793)
# Signing numbers
'{:+d}'.format(42)
'{:d}'.format(-42)
# Placeholders 
data = {'name': 'John', 'password': '<PASSWORD>'}
print('Username is {name} and password is {password}'.format(**data))
print('Username is {name} and password is {password}'.format(name='Opensource',password='<PASSWORD>'))<jupyter_output>Username is John and password is <PASSWORD>
Username is Opensource and password is <PASSWORD><jupyter_text>Get Items to format<jupyter_code>person = {'first': 'Hello', 'last': 'User'}
'{p[first]} {p[last]}'.format(p=person)
data = [4, 8, 15, 16, 23, 42]
'{d[4]} {d[5]}'.format(d=data)
class Plant(object):
    type = 'tree'
    
'{p.type}'.format(p=Plant())
class Plant(object):
    type = 'tree'
    kinds = [{'name': 'oak'}, {'name': 'maple'}]
    
'{p.type}: {p.kinds[0][name]}'.format(p=Plant())
from datetime import datetime
'{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))<jupyter_output><empty_output><jupyter_text>STRING OPERATIONS<jupyter_code># Single quotes
'Isn\'t'
"Isn't"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19582850521436848, 'fraction_numerical': 0.03901120123599845, 'mean_word_length': 6.708333333333333, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '229675', 'license': 'MIT', 'n_tokens_mistral': 1034, 'n_tokens_neox': 965, 'n_words': 209, 'path': 'Python-Formating.ipynb', 'repo_name': 'vvscloud/python-notes', 'size': '2577'}