<jupyter_start><jupyter_text>StringsAny set of characters enclosed in single or double or triple quotestriple quotes are used for the multi line stringsEvery data type in python has standard fucntions called built in fucntions these fucntions you get when you install the functiondir(__ __builtins__ __) provide the list of the built functions the __ XXX __ are special variables and they also show erros and then the list is the built in fucntionsdir(__ __str__ __) provide the list of the built string metods Operations1.   Slicing -- String[range]2.   Updating -- String[range]+'x'3.   Concatenation -- String 1 + String 24.   Repetition -- String 1 * n5.   Membership -- In, not in6.   Reverse -- String[:-1]<jupyter_code>print("hello world")
print('Vijay')
print('''Hello triple single quotes''')
print("""Hello triple double quotes
multi lines
""")<jupyter_output>Hello triple double quotes
multi lines<jupyter_text>Escape sequencesThere are various sequences provided in python that can be used to display quotes, neline,Code | Result--- | ---\\' | Single quote\\\ | Backslash\n | New line\r | Carriage Return\t | Tab\b | Backspace\f | Form Feed\ooo | Octal value\xhh | Hex value<jupyter_code># New line and single quote
x = 'Hello\nVijay, it\'s correct'
print(x)

# Backslash
print('c:\\temp\\')

print ('\n')

# carriage return
print("hello\rVijay")


y = 'hello \r Vijay'
print(y)

z = 'hello \t Vijay'
print(y)
# carriage return
print("hello\rVijay")


y = 'hello \r Vijay'
print(y)
txt = "\110\145\154\154\157"
print(txt) 
#A backslash followed by an 'x' and a hex number represents a hex value:
txt = "\x48\x65\x6c\x6c\x6f"
print(txt) 
# Take the comma separated values as input and create a list 

x = input('Enter the names separated by comma').split(',')
print(x)
#capitilise the names
print(x[0].capitalize())
# Some more fucntions of the string
'''
1. startswith
2. endswith
3. index
4. 

'''

sentence1 = '''Happiness is a state of mind and the feeling expressed when things are going great. 
It is what we feel when we get our first car, buy a new house or graduate with the best grades. 
Happiness should be distinguished from joy. When joy is a constant state of mind, 
happiness depends on events in our lives.'
'''

print('Sentence1 start with :', sentence1.startswith('Happiness'))
print('Sentence1 Startswith \'Happiness\': %s'%sentence1.startswith('Happiness'))

#index
print('index of gradutae : ', sentence1.index('graduate'))

#slicing
print(sentence1[150:158])
print('\n')
print(sentence1[150::])
print('\n')
# reverse
print(sentence1[::-1])
print('\n')
# reverse
print(sentence1[150:-16])<jupyter_output>Sentence1 start with : True
Sentence1 Startswith 'Happiness': True
index of gradutae :  150
graduate


graduate with the best grades. 
Happiness should be distinguished from joy. When joy is a constant state of mind, 
happiness depends on events in our lives.'




'.sevil ruo ni stneve no sdneped ssenippah
 ,dnim fo etats tnatsnoc a si yoj nehW .yoj morf dehsiugnitsid eb dluohs ssenippaH
 .sedarg tseb eht htiw etaudarg ro esuoh wen a yub ,rac tsrif ruo teg ew nehw leef ew tahw si tI
 .taerg gniog era sgniht nehw desserpxe gnileef eht dna dnim fo etats a si ssenippaH


graduate with the best grades. 
Happiness should be distinguished from joy. When joy is a constant state of mind, 
happiness depends on events<jupyter_text>string.replace How it works<jupyter_code>sentence1.replace('graduate', 'post graduate')
print(sentence1) # no change in the sentence because the strings re immutable we need to assign to new one

# to make the replace work 
sentence2 = sentence1.replace('graduate', 'post graduate')
print(sentence2)<jupyter_output>Happiness is a state of mind and the feeling expressed when things are going great. 
It is what we feel when we get our first car, buy a new house or graduate with the best grades. 
Happiness should be distinguished from joy. When joy is a constant state of mind, 
happiness depends on events in our lives.'

Happiness is a state of mind and the feeling expressed when things are going great. 
It is what we feel when we get our first car, buy a new house or post graduate with the best grades. 
Happiness should be distinguished from joy. When joy is a constant state of mind, 
happiness depends on events in our lives.'<jupyter_text>Find out if the string1.   numeric2.   alphabet<jupyter_code>text=input('String : ')
print(text.isalpha())
print(text.isnumeric())<jupyter_output>String : vijay
True
False<jupyter_text>Change the case - upper and lower<jupyter_code>print(text)
print(text.upper())
print(text.lower())
print(text.capitalize())
print(text)<jupyter_output>vijay
VIJAY
vijay
Vijay
vijay<jupyter_text>Format function<jupyter_code>print('my name is: {0} and I live in {1}'.format(input('Name :'),input('City :' )))<jupyter_output>Name :Vijay
City :Stevenage
my name is: Vijay and I live in Stevenage<jupyter_text>String encode and decode function string encodeThe Python string encode() function is used to encode a string using the encoding specified. The bytes object is returned by this function. If no encoding is specified, the "utf-8" encoding is used as the default. string decodeThe bytes decode() function in Python is used to convert bytes into string objects.Both of these functions allow us to specify the error handling scheme that will be used for encoding and decoding errors. The default is **'strict,'** which means that encoding errors result in a UnicodeEncodeError. Other possible values include 'ignore,"replace,' and 'xmlcharrefreplace.'<jupyter_code># Sender side
str1 = 'Vijay'
#check the type of the string
print(type(str1))
# encode 
str1_encode= str1.encode('utf-8')
print(str1_encode)
print(type(str1_encode))
# Recever side
result=str1_encode.decode('utf-8')
print(type(result))
print(result)
# Another example
str2 = input('Enter string data : ')
str2_encode = str2.encode()
str2_decode = str2_encode.decode()

print('Bytes encoded : ', str2_encode)
print('decoded string :', str2_decode )<jupyter_output>Enter string data : aåb∫cçd∂e´´´ƒg©1¡
Bytes encoded :  b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
decoded string : aåb∫cçd∂e´´´ƒg©1¡<jupyter_text>Difference between find() and index() in Python1.   If a substring isn't found in a string, find() returns -1 and index() throws a ValueError.2.   A substring found in a string can be used to make certain statements run. This means that find() can be used in conditional statements, such as "if," "if-else," and "if-elif." This method can't be used in conditional statements, though, because it will cause a mistake to be made.3.   find() can only be used with strings, but index() can be used with lists, tuples, and strings as well, so it can be used with both.<jupyter_code># This will return -1
print('Vijay'.find('c'))
# This will return error
print("Vijay".index('c'))
# get the list of the builtin fucntions
dir(__builtins__)
## String methods
dir(str)
x = 'HELLO world how are you. hope you are well'

print('Converts the sentence to lower case --- ', x.lower())
print('Converts the sentence to upper case --- ', x.upper())
print('Converts the first letter of the sentence to upper, but does not recognize the full stop --- ', x.capitalize())
print('Count of o (lower case) --- ', x.count('o') )
print('Count of O (Upper case) --- ', x.count('O') )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10071457462585952, 'fraction_numerical': 0.016988000539301603, 'mean_word_length': 4.799843627834245, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '283653', 'license': 'MIT', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2372, 'n_words': 943, 'path': '04_Strings.ipynb', 'repo_name': 'vijaypattar28/Python_Training', 'size': '7417'}