Format name from input using string slicing - python

I have an assignment that I need help with.
In my program, I am prompting users to enter their fullname in first, middle, and last name order.
fullname = input("Enter your full name in first, middle, and last name order: ")
From the fullname entered, I need to pull off the first character of the first name and pull off the last name into separate variables, and I am not sure how to proceed.
I cannot use the split function for this. Instead, I must use the String slicing technique.
Any help would be much appreciated! Thanks in advance!

you can get the first inital by slicing the string at index 0 since this will be the first char. for the last name you can do an rfind on the string to find the first space from the right side of the string. Then slice from the first char after that to the end of the string for the last name
fullname = input("Enter your full name in first, middle, and last name order: ")
print(f"{fullname[0]} {fullname[fullname.rfind(' ')+1:]}")
OUTPUT
Enter your full name in first, middle, and last name order: foo bar baz
f baz

Related

Parsing data from the squished string

I need to write a pattern using Regex, which from the string "PriitPann39712047623+372 5688736402-12-1998Oja 18-2,Pärnumaa,Are" will return a first name, last name, id code, phone number, date of birth and address. There are no hard requirements beside that both the first and last names always begin with a capital letter, the id code always consists of 11 numbers, the phone number calling code is +372 and the phone number itself consists of 8 numbers, the date of birth has the format dd-mm-yyyy, and the address has no specific pattern.
That is, taking the example above, the result should be [("Priit", "Pann", "39712047623", "+372 56887364", "02-12-1998", "Oja 18-2,Parnumaa,Are")]. I got this pattern
r"([1-9][0-9]{10})(\+\d{3}\s*\d{7,8})(\d{1,2}\ -\d{1,2}\-\d{1,4})"
however it returns everything except first name, last name and address. For example, ^[^0-9]* returns both the first and last name, however I don't understand how to make it return them separately. How can it be improved so that it also separately finds both the first and last name, as well as the address?
The following regex splits each of the fields into a separate group.
r"([A-Z]+[a-z]+)([A-Z]+[a-z]+)([0-9]*)(\+372 [0-9]{8,8})([0-9]{2,2}-[0-9]{2,2}-[0-9]{4,4})(.*$)"
You can get each group by calling
m = re.search(regex, search_string)
for i in range(num_fields):
group_i = m.group(i)

Giving different parts of an input value

I have a task to rearrange a name to last first middle initial. I know for this I can use split() but I'm trying to understand it the way I'm learning it right now which is index and find ect to rearrange it. My question is how would I make it so the program knows what is the first last and middle names since it changes depending on user input. I've tried this and it doesn't work. Is there a way to do this?
Name = input("Enter a name like 'First I. Last: ")
words = Name.find(" ")
first, middle, last = words[1], words[0], words[-1]
find will return the index into a string of that occurence, you can then use that index to slice your original string, find also takes an optional second index to tell it where to start searching from ...
Name = input("Enter a name like 'First I. Last: ")
first_space_index = Name.find(" ")
first_name = Name[:first_space_index]
# find the first space that comes after first_space_index
second_space_index = Name.find(" ",first_space_index + 1)
middle_initial = Name[first_space_index+1:second_space_index]
this is not nearly as good of a solution as just using split but meh ...
You can use the string.split() function to gather the different words in the input. This is preferable to using string.find() as you do not have to slice to find the answer.
name = input("Enter a name like 'First I. Last: ")
words = name.split()
first, middle, last = name[0], name[1], name[2]
Then you can work out the first letter of each 'Hello'[0] -> 'H'

How to keep a variable in a string from changing python

I'm new to python and I've got an assignment to Write a program that inputs a string from the user and than to print the string in which all instances of the first character have been replaced by an 'e' except for the first character itself.
This is what I got so far:
sent = input('Please enter a string: ')
var1 = sent[0]
var2 = 'e'
mod_sent = sent.replace(var1,var2)
print(mod_sent)
I know that nothing there is supposed to keep the first character from changing but I feel like I have tried everything and just have to delete because it is not working.
Would like an explanation and just an answer if possible please.
Actually, the first character is changing in the sent.replace you called, because python replaces every character in the string with the substitute.
You can just revert the first character, which should be easy since you stored it in var1 (try using more descriptive names next time).
mod_sent = var1 + mod_sent[1:]
Add this before your print statement and it should work.
You can slice the string str[1:] will return the whole string except first character:
sent = input('Please enter a string: ')
var1 = sent[0]
var2 = 'e'
mod_sent = var1 + sent[1:].replace(var1,var2)
print(mod_sent)

How do you ask a user for their name and display the first and last character in python?

I tried using this piece of code:
name = input("What is your full name? ")
print(name[0:-1])
but it didn't work. Instead of displaying the first and last character, it would delete the last letter and display the rest.
Any help would be appreciated.
If you want to do it without declaring any variables you could use itemgetter() and then join first and last returned value.
from operator import itemgetter
print(''.join(itemgetter(0,-1)(input("What is your full name? "))))
Without importing libraries
print(''.join(map(input("What is your full name? ").__getitem__, (0,-1))))
or as #Graipher suggested:
print((lambda s: s[0] + s[-1])(input("What is your full name? ")))
Output:
What is your full name? hello
ho
You could just do:
name = input("What is your full name? ")
print(name[0] + name[len(name)-1])
Putting 0 would display the first letter and because "len" counts the number of letters and the variable (name) already has stored information, subtracting 1 would give you the last character.

Swapping first and last names removing commas

Using python 3.3:
I need some help in writing the body for this function that swaps the positions of the last name and first name.
Essentially, I have to write a body to swap the first name from a string to the last name's positions.
The initial order is first name followed by last name (separated by a comma). Example: 'Albus Percival Wulfric Brian, Dumbledore'
The result I want is: 'Dumbledore Albus Percival Wulfric Brian'
My approach was:
name = 'Albus Percival Wulfric Brian, Dumbledore
name = name[name.find(',')+2:]+", "+name[:name.find(',')]
the answer I get is: 'Dumbledore, Albus Percival Wulfric Brian' (This isn't what I want)
There should be no commas in between.
I'm a new user to Python, so please don't go into too complex ways of solving this.
Thanks kindly for any help!
You can split a string on commas into a list of strings using syntax like astring.split(',')
You can join a list of strings into a single string on whitespace like ' '.join(alist).
You can reverse a list using list slice notation: alist[::-1]
You can strip surrounding white space from a string using astring.strip()
Thus:
' '.join(aname.split(',')[::-1]).strip()
You're adding the comma in yourself:
name = name[name.find(',')+2:] + ", " + name[:name.find(',')]
Make it:
name = name[name.find(',')+2:] + name[:name.find(',')]

Categories

Resources