<jupyter_start><jupyter_text>CHAPTER 6 - STRINGS  6.1 A string is a sequence<jupyter_code>fruit = 'banana'
letter = fruit[0]
print(letter)
letter = fruit[1]
print(letter)
letter = fruit[1.5]<jupyter_output><empty_output><jupyter_text>String Indexesb  -  a  -  n  -  a  -  n  -  a[0] - [1] - [2] - [3] - [4] - [5]  6.2 Getting the length of a string using len<jupyter_code>fruit = 'banana'
len(fruit)
length = len(fruit)
last = fruit[length]
# To get the last character, you have to subtract 1 from length
last = fruit[length-1]
print(last)<jupyter_output>a<jupyter_text>6.3 Traversal through a string with a loop<jupyter_code># using while loop

index = 0
while index < len(fruit):
    letter = fruit[index]
    print(letter)
    index = index + 1
# using for loop

for char in fruit:
    print(char)<jupyter_output>b
a
n
a
n
a<jupyter_text>6.4 String slices<jupyter_code>s = '<NAME>'
print(s[0:5])
print(s[6:12])
fruit = 'banana'
fruit[:3]
fruit[3:]
fruit[3:3]
fruit[:]<jupyter_output><empty_output><jupyter_text>6.5 Strings are immutable<jupyter_code>greeting = 'Hello, world!'
greeting[0] = 'J'
new_greeting = 'J' + greeting[1:]
print(new_greeting)

# This concatenates a new first letter onto a slice of greeting. It has no effect on the original string<jupyter_output>Jello, world!<jupyter_text>6.6 Looping and counting<jupyter_code># The following counts the number of times the letter " a " appears in a string

word = 'banana'
count = 0
for letter in word:
    if letter == 'a':
        count = count + 1
print(count)<jupyter_output>3<jupyter_text>6.7 The in operator<jupyter_code>'a' in 'banana'
'seed' in 'banana'<jupyter_output><empty_output><jupyter_text>6.8 String comparison<jupyter_code>word = 'banana'
if word == 'banana':
    print('All right, bananas.')
word = 'Banana'    # Comparision done letter by letter, considering the ASCII values
if word < 'banana':
    print('Your word, ' + word + ', comes before banana.')
elif word > 'banana':
    print('Your word, ' + word + ', comes after banana.')
else:
    print('All right, bananas.')
word = 'pineapple'    # Comparision done letter by letter, considering the ASCII values
if word < 'banana':
    print('Your word, ' + word + ', comes before banana.')
elif word > 'banana':
    print('Your word, ' + word + ', comes after banana.')
else:
    print('All right, bananas.')
word = 'Pineapple'    # Comparision done letter by letter, considering the ASCII values
if word < 'banana':
    print('Your word, ' + word + ', comes before banana.')
elif word > 'banana':
    print('Your word, ' + word + ', comes after banana.')
else:
    print('All right, bananas.')<jupyter_output>Your word, Pineapple, comes before banana.<jupyter_text>6.9 string methods<jupyter_code>stuff = 'Hello world'
type(stuff)
dir(stuff)     # built in functions for a string 
help(str.capitalize)
word = 'banana'
new_word = word.upper()
print(new_word)
word = 'banana'
index = word.find('a')
print(index)
word.find('na')  # find method can find substrings as well as characters
word.find('na', 3)  # can take as a second argument the index where it should start
# remove white space (spaces, tabs, or newlines) from the beginning and end of a string using the strip method
line = ' Here we go '
line.strip()
line = 'Have a nice day'
line.startswith('Have')
line.startswith('h')
line.lower()
line.lower().startswith('h')<jupyter_output><empty_output><jupyter_text>6.10 Parsing strings<jupyter_code>data = 'From <EMAIL> Sat Jan 5 09:14:16 2008'
atpos = data.find('@')
print(atpos)
sppos = data.find(' ',atpos)
print(sppos)
host = data[atpos+1:sppos]
print(host)<jupyter_output>uct.ac.za<jupyter_text>6.11 Format operator<jupyter_code>camels = 42
'%d' % camels
camels = 42
'I have spotted %d camels.' % camels
'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')
'%d %d %d' % (1, 2)
'%d' % 'dollars'<jupyter_output><empty_output><jupyter_text>6.12 Debugging<jupyter_code>while True:
    line = input('> ')
    if line[0] == '#':
        continue
    if line == 'done':
        break
    print(line)
print('Done!')<jupyter_output>> Hello there !
Hello there !
> # dont print this !
> print this :)
print this :)
>  
 
> done
Done!
{'fraction_non_alphanumeric': 0.14083493282149712, 'fraction_numerical': 0.018953934740882916, 'mean_word_length': 4.5438829787234045, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 53, 'https://': 0, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '576346', 'license': 'MIT', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1523, 'n_words': 444, 'path': 'CHAPTER 6.ipynb', 'repo_name': 'AbhishekMali21/PYTHON-FOR-EVERYBODY', 'size': '4193'}