473,799 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2828
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
2624
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
6035
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 paragraphs. So far, so good: some of the paragraphs begin with a link, and there are styles in the stylesheet for link, hover etc. The first letter is still displayed as styled for the paragraph's first-letter.
5
3862
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 the fifth letter) How do you 'translate' this sentence into VB.net? Thanks! -- Xero
8
2493
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 going to school. See you in the afternoon. Is there any functions to do this job? Thanks, Scott
4
13885
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 result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated. import java.util.StringTokenizer; import java.util.Scanner; public class Main { public static void...
3
5070
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 sentence, using JOptionPane.showInputDialog(). 2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears. 3. Using a JOptionPane.showInputDialog(),...
4
6563
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 a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442)." how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (? also how do i use the...
12
4005
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
5041
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
6032
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 getFirsts() function I get a breakpoint in a file in the compiler. class charconverter { private: int count; int last; public:
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.