<jupyter_start><jupyter_text>Strings     A string is a sequence of characters.    Computers do not deal with characters, they deal with numbers (binary). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0's and 1's.    This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encoding used.    In Python, string is a sequence of Unicode character. For more details about unicode https://docs.python.org/3.3/howto/unicode.html  How to create a string? Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.<jupyter_code>myString = 'Hello'

print(myString)


myString = "Hello"
print(myString)


myString = '''Hello'''
print(myString)<jupyter_output>Hello
Hello
Hello<jupyter_text>How to access characters in a string? We can access individual characters using indexing and a range of characters using slicing.Index starts from 0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError.Python allows negative indexing for its sequences.<jupyter_code>myString = "Hello"

#print first Character
print(myString[0])

#print last character using negative indexing
print(myString[-1])

#slicing 2nd to 5th character
print(myString[2:5])<jupyter_output>H
o
llo<jupyter_text>If we try to access index out of the range or use decimal number, we will get errors.<jupyter_code>print(myString[15])
print(myString[1.5])<jupyter_output><empty_output><jupyter_text>How to change or delete a string ? Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.<jupyter_code>myString = "Hello"
myString[4] = 's' # strings are immutable<jupyter_output><empty_output><jupyter_text>We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword       del.<jupyter_code>del myString # delete complete string
print(myString)<jupyter_output><empty_output><jupyter_text>String Operations  Concatenation Joining of two or more strings into a single one is called concatenation.The + operator does this in Python. Simply writing two string literals together also concatenates them.The * operator can be used to repeat the string for a given number of times.<jupyter_code>s1 = "Hello "
s2 = "Satish"

#concatenation of 2 strings
print(s1 + s2)

#repeat string n times
print(s1 * 3)<jupyter_output>Hello Satish
Hello Hello Hello<jupyter_text>Iterating Through String<jupyter_code>count = 0
for l in "Hello World":
    if l == 'o':
        count += 1
print(count, ' letters found')<jupyter_output>2  letters found<jupyter_text>String Membership Test<jupyter_code>print('l' in 'Hello World') #in operator to test membership
print('or' in 'Hello World')<jupyter_output>True<jupyter_text>String Methods     Some of the commonly used methods are lower(), upper(), join(), split(), find(), replace() etc<jupyter_code>"Hello".lower()
"Hello".upper()
"This will split all words in a list".split()
' '.join(['This', 'will', 'split', 'all', 'words', 'in', 'a', 'list'])
"Good Morning".find("Mo")
s1 = "Bad morning"

s2 = s1.replace("Bad", "Good")

print(s1)
print(s2)<jupyter_output>Bad morning
Good morning<jupyter_text>Python Program to Check where a String is Palindrome or not ?<jupyter_code>myStr = "Madam"

#convert entire string to either lower or upper
myStr = myStr.lower()

#reverse string
revStr = reversed(myStr)


#check if the string is equal to its reverse
if list(myStr) == list(revStr):
    print("Given String is palindrome")
else:
    print("Given String is not palindrome")<jupyter_output>Given String is palindrome<jupyter_text>Python Program to Sort Words in Alphabetic Order?<jupyter_code>myStr = "python Program to Sort words in Alphabetic Order"

#breakdown the string into list of words
words = myStr.split()

#sort the list
words.sort()

#print Sorted words are
for word in words:
    print(word)<jupyter_output>Alphabetic
Order
Program
Sort
in
python
to
words
{'fraction_non_alphanumeric': 0.08341078066914498, 'fraction_numerical': 0.007202602230483271, 'mean_word_length': 5.463963963963964, 'pattern_counts': {'":': 1, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '22705', 'license': 'MIT', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1265, 'n_words': 526, 'path': 'Python Programs for YouTube/2_Data Structure/5_Strings.ipynb', 'repo_name': 'charanhu/CRTEK', 'size': '4304'}