Skip to content

Instantly share code, notes, and snippets.

@daindeutschman
Last active June 4, 2021 18:26
Show Gist options
  • Save daindeutschman/af536a30f98821008df6f0b45efc323b to your computer and use it in GitHub Desktop.
Save daindeutschman/af536a30f98821008df6f0b45efc323b to your computer and use it in GitHub Desktop.
zybooks-6-strings
# ////6.1 String slicing////
# my_str[5] reads the character in position 5 of the string my_str.
# Multiple consecutive characters can be read using slice notation my_str[start:end]
# Creates a new string whose value mirrors the characters of my_str from positions start to end - 1.
# If my_str is 'Boggle', then my_str[0:3] yields string 'Bog' (B0 o1 g2 g3 - 1 is Bog)
# Negative indices can be used to specify start or end positions relative to the end of the string.
# Example: If string is 'Jane Doe!?', then my_str[0:-2] yields 'Jane Doe'
# Partial notation: my_str[:5] reads positions 0-4.
# Partial notation: my_str[5:] yields all characters at and after position 5.
################################
# Figure 6.1.1: String slicing.
url = 'http://en.wikipedia.org/wiki/Turing'
domain = url[7:23] # Read 'en.wikipedia.org' from url
print(domain)
################################
################################
# Figure 6.1.2: A slice creates a new object.
# Creating a slice of the string variable my_str, and then changing the value of my_str, does not also change the value of the slice.
my_str = "The cat jumped the brown cow"
animal = my_str[4:7]
print('The animal is a %s' % animal)
my_str = 'The fox jumped the brown llama'
print('The animal is still a', animal) # animal variable remains unchanged.
# Output:
# The animal is a cat
# The animal is still a cat
################################
################################
# 6.1.4: Slicing example: omitting start, end positions using variables
usr_text = input('Enter a string:\n')
first_half = usr_text[:len(usr_text)//2]
last_half = usr_text[len(usr_text)//2:]
print('The first half of the string is "%s"' % first_half)
print('The second half of the string is "%s"' % last_half)
################################
################################
"""
Table 6.1.1: Common slicing operations/Example slicing operations
my_str[10:19] wikipedia #Gets the characters in positions 10-18.
my_str[10:-5] wikipedia.org/wiki/ #Gets the characters in positions 10-28.
my_str[8:] n.wikipedia.org/wiki/Nasa/ #All characters from position 8 until the end of the string.
my_str[:23] http://en.wikipedia.org #Every character up to position 23, but not including my_str[23].
my_str[:-1] http://en.wikipedia.org/wiki/Nasa #All but the last character.
my_str[:] http://en.wikipedia.org/wiki/Nasa/ #A new copy of the my_str object.
Write a statement that assigns str2 with a copy of str1.
str2 = str1[:]
"""
################################
################################
# Stride: The stride determines how much to increment the index after reading each element.
# For example, my_str[0:10:2] reads every other element between 0 and 10. The stride defaults to 1 if not specified.
# Figure 6.1.3: Slice stride.
numbers = '0123456789'
print('All numbers: %s' % numbers[::])
print('Every other number: %s' % numbers[::2])
print('Every third number between 1 and 8: %s' % numbers[1:9:3])
#Output:
# All numbers: 0123456789
# Every other number: 02468
# Every third number between 1 and 8: 147
################################
################################
#6.1.1: Slice a rhyme Challenge Activity
start_index = 4
end_index = 7
rhyme_lyric = 'The cow jumped over the moon.'
sub_lyric = rhyme_lyric[start_index:end_index]
print(sub_lyric)
################################
# ////6.2 Advanced string formatting////
#Construct 6.2.1: Conversion operators
'age is %d' % user_age
# %d - decimal
# %f - float
# %s - string
#### Text alignment and float precision ####
## Minimum field width:
# placed immediately before the conversion type, that describes the minimum number of characters to be inserted
# in the string. If a string value assigned to a conversion specifier is smaller in size than the specified minimum
# field width, then the left side of the string is padded with space characters
# Output is: Student name ( Bob)
print('Student name (%5s)' % 'Bob')
## Conversion Flags:
# alter the output of conversion specifiers.
# If the '0' conversion flag is included, numeric conversion types (%d, %f) add the leading 0s prescribed
# by the minimum field width in place of spaces
## Figure 6.2.1: String formatting example: Add leading 0s by setting the minimum field width and 0 conversion flag.
# Output is:
# Enter student ID: 1234
# The user entered 1234
# Full 8-character student ID: 00001234
student_id = int(input('Enter student ID: '))
print('The user entered %d' % student_id)
print('Full 8-character student ID: %08d' % student_id)
## Conversion Specifier
# The optional precision component of a conversion specifier indicates how many digits
# to the right of the decimal should be included
## Figure 6.2.2: String formatting example: Setting precision of floating-point values.
import math
# Output is:
# pi is 3.141593.
# 22/7 is 3.142857.
# 22/7 is accurate for 2 decimal places: 3.14
real_pi = math.pi # math library provides close approximation of pi
approximate_pi = 22.0 / 7.0 # Approximate pi to 2 decimal places
print('pi is %f.' % real_pi)
print('22/7 is %f.' % approximate_pi)
print('22/7 is accurate for 2 decimal places: %.2f' % approximate_pi)
@nealbrittany52
Copy link

Assign sub_lyric with 'cow' by slicing rhyme_lyric from start_index to end_index. Sample output from the given program:
cow

I need you to help with this Zybooks Python Programming

@nealbrittany52
Copy link

it is in Figure 8.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment