473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

removing a letter from a sentence

I am trying to remove every other letter from a sentence that I input just by using a for loop. This code however just reprints the sentence
Expand|Select|Wrap|Line Numbers
  1. def removeLetter(sentence):
  2.     index=0
  3.     for letter in sentence:
  4.         index=index+1
  5.         if index%2==0:
  6.             letter=""
  7.             sentence=sentence+letter
  8.             return sentence
Oct 1 '10 #1
2 2807
bvdet
2,851 Expert Mod 2GB
You assign letter to a null string and then add it to sentence, in effect doing nothing. The following code uses a list comprehension to create a list of the letters you want to keep, then joins the letters into a new abbreviated sentence.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "This is a test sentence."
  2. >>> "".join([letter for i, letter in enumerate(s) if not i%2])
  3. 'Ti sats etne'
  4. >>> 
Oct 1 '10 #2
dwblas
626 Expert 512MB
You have to add the desired letters to a new string, or convert the original string to a list. Note also that the return statement doesn't have the correct indent.
Expand|Select|Wrap|Line Numbers
  1. def removeLetter(sentence):
  2.     index=0
  3.     new_sentence = ""
  4.     for letter in sentence:
  5.         index=index+1
  6.         if index%2==0:
  7.             new_sentence += letter
  8.     return new_sentence 
Oct 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Noud Aldenhoven | last post by:
Hello everyone, I was wondering how to remove comments away form a file. So that's why I made this script. =============================== #!/usr/bin/env python import sys import string
12
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on...
5
by: Xero | last post by:
How do you get a specific letter from a word? For example, in English, I would say: Get the fifth letter from the word "Computer" Then the letter 'u' will be returned. (C-O-M-P-U-..., 'u' is...
8
by: Scott | last post by:
I would like to automatically change the first letter to upper case and keep the rest intact of each sentence on a control of a form, i.e., i am going to school. see you in the afternoon. -I am...
4
evilmonkey
by: evilmonkey | last post by:
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same...
3
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. Program should.... 1. Ask user to type in a...
4
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by...
12
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
3
by: dmalhotr2001 | last post by:
Hi, For string extraction function in vb, if I feed in a paragraph, how do I extract the first sentence of that paragraph. Thanks :D
6
by: TimSama | last post by:
The below code is to make a sentence upper case, and also get every first letter of every word in it, and put it in a different string to be called by the main function. When I try and call the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.