Summary
This lesson covered using string and string functions in python and javascript
Python:
- upper() - Converts all characters in the string to uppercase.
- lower() - Converts all characters in the string to lowercase.
- capitalize() - Capitalizes the first character of the string.
- replace() - Replaces a substring with another substring.
- split() - Splits the string into a list of substrings based on a delimiter.
- join() - Joins a list of strings into a single string with a specified delimiter.
- isnumeric() - Checks if all the characters in the string are numeric (0-9).
Functions:
- len() - Returns the length of the string.
- str() - Converts a value to a string.
- f-strings - Formats strings using placeholders inside curly braces.
- find() - Returns the lowest index of the substring if it is found in the string.
Javascript
- Concatenation: chains strings together, can use + to do so
- Substrings: smaller part of a string
- length: Finds length of a string
# Python examples
# Popcorn Hack Python
# Finding number of characters in last name using Len.
yay = len("Kandula")
print(yay) # Output is 7
# Using concat to merge first and last name
name = "Soumini " + "Kandula"
print(name) # Output is Soumini Kandula
# Using substring to show 3rd to 6th letters of my name
substring = name[2:6]
print(substring) # Output is umin
# Homework Python
def character_count(input_string):
total_letters = 0
total_numbers = 0
total_spaces = 0 # sets all variables to 0
for char in input_string:
if char.isalpha():
total_letters += 1 # If the character in the input string is a letter, the letter count increases by 1
elif char.isdigit():
total_numbers += 1 # If character in string is a number, the number variable increases by 1
elif char.isspace():
total_spaces += 1 # if the character in the string is a space, the space variable increases by 1
return total_letters, total_numbers, total_spaces #returns the final number of letters, numbers, and spaces
# Get user input
user_input = input("Enter string: ") # gets input
letters, numbers, spaces = character_count(user_input)
# Print the results
print(f"Total letters: {letters}") # Prints number of letters
print(f"Total numbers: {numbers}") # Prints number of numbers
print(f"Total spaces: {spaces}") # prints number of spaces
// Javascript Examples
// JS Popcorn Hack
let last_name = "Kandula"; // setting last_name equal to Kandula
let the_length = last_name.length;
console.log(the_length) // Output would be 7
let a_name = "Soumini " + "Kandula"
console.log(a_name) // Output would be Soumini Kandula
let a_substring = a_name.substring(1,8)
console.log(a_substring) // Output would be oumini K
// JS homework
function characterCount(inputString) {
let totalLetters = 0;
let totalNumbers = 0;
let totalSpaces = 0; // sets all variables to 0
for (let char of inputString) {
if (char.match(/[a-zA-Z]/)) {
totalLetters += 1; // If the character is a letter, increase the letter count
} else if (char.match(/[0-9]/)) {
totalNumbers += 1; // If character is a number, increase the number count
} else if (char == ' ') {
totalSpaces += 1; // If the character is a space, increase the space count
}
}
return [totalLetters, totalNumbers, totalSpaces]; // returns the counts in an array
}
// Get user input
const userInput = prompt("Enter string:"); // gets input
const [letters, numbers, spaces] = characterCount(userInput); // Call the function
// Print the results
console.log(`Total letters: ${letters}`); // Prints number of letters
console.log(`Total numbers: ${numbers}`); // Prints number of numbers
console.log(`Total spaces: ${spaces}`); // Prints number of spaces